简体   繁体   中英

How to define a different bold font in Android textview

I need to use a different font for the bold elements in HTML text rendered in an Android textview.

I tried doing it this way, but it looks like the Typeface style argument just sets the entire textview to normal or bold, rather than setting the font for that style in the textview.

TextView tv = (TextView) dialogView.findViewById(R.id.textInstructions);
String instructions = "<b>Headline</b><br/>Regular Text";
tv.setText(Html.fromHtml(instructions));

Typeface font = Typeface.createFromAsset(BaseActivity.this.getAssets(), "OpenSans-Regular.ttf");
Typeface bold = Typeface.createFromAsset(BaseActivity.this.getAssets(), "OpenSans-Bold.ttf");
tv.setTypeface(font, Typeface.NORMAL);
tv.setTypeface(bold, Typeface.BOLD);

Looks like this stackoverflow question answers it, it requires one to merge the two true type fonts into one:

Html in text view with different fonts for bold and italic

But I haven't been able to find instructions on how to merge the fonts in ttfedit. I opened the utility but am at a loss on how to use it.

Looks like this stackoverflow question answers it, it requires one to merge the two true type fonts into one:

Html in text view with different fonts for bold and italic

tv.setText(Html.fromHtml("<html><body><font size=5 color=red>Hello </font> World </body><html>"));

尝试这个。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM