Question

I'm having a little trouble getting my custom font to show up bold, here's what I'm trying to do:

I've set an HTML formatted string in res/string

<string name="string1">
<![CDATA[<b>Title1</b><p>sub text</p>]]>
</string> 

Then I set the font at runtime (where getString gets a resID from a string):

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/eurostileltstddemi.ttf"); 
TextView body = (TextView) findViewById(R.id.tv_body);
body.setText(Html.fromHtml(getString(name)));
body.setTypeface(tf);

Problem being that the text doesn't show up as bold where it should, any ideas where I'm going wrong??

Sorry forgot to add that I need some text as bold and some not.

Était-ce utile?

La solution

Use:

body.setTypeface(tf, Typeface.BOLD);

Typeface

Also you can set typeface on webView. Just add something like this:

<html>  <head><style type=\"text/css\">  @font-face {  font-family: MyFont;      src: url(\"file:///android_asset/font.ttf\")  }    body { font-family: MyFont;  font-size: 12px;  text-align: justify;   }   </style> </head><body>

and load using:

webView.loadDataWithBaseURL("file:///android_asset/", result, "text/html", "UTF-8", "null");

//FOR EDITED QUESTION

String s = getResources().getString(R.string.string1);
((TextView) findViewById(R.id.t)).setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);

Autres conseils

I've tried according to your code with some changes.

Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/eurostileltstddemi.ttf"); 
TextView body = (TextView) findViewById(R.id.myTV);
body.setText(Html.fromHtml(getString(R.string.string1)));
body.setTypeface(tf);

It should work. And FYR, the correct way to access the String resource is like R.string.your_string . Ref String Resources.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top