Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android show HTML text as bold with custom font

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.

like image 720
Isaac Avatar asked Aug 20 '13 10:08

Isaac


1 Answers

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);
like image 78
Adam Radomski Avatar answered Sep 27 '22 18:09

Adam Radomski