简体   繁体   中英

Html in text view with different fonts for bold and italic

I'm trying to use a custom font on a TextView . The TextView text is set with textView1.setText(Html.fromHtml(htmlText));

The html contains bold and italic spans

Now. I purchased a custom font. The font comes with 3 different files (ttf). One for regular, one bold and for italic.

How can I apply those three font files to the textview?

This link will help you to see how to customize android font: http://mobile.tutsplus.com/tutorials/android/customize-android-fonts/

In which concerns how to apply those font files to the textview, you need to integrate them first in your project:

Typeface tf = Typeface.createFromAsset(this.getAssets(),
        "fonts/xxx.TTF");
txt1.setTypeface(tf);

The ttf file should be placed in --> assets/fonts/xxx.TTF

All needed details are in the paragraph: "Using Custom Fonts"

I imagine you want to do a quick refactor on your code in order to incorporate the assets.

I would extend TextView and attempt to parse the HTML and apply the proper typeface at onDraw.

Override setText and parse the parameter creating a Map for character and the proper typeface that should be used.

Then, override onDraw and before drawing, change the typeface of super.getPaint() in accord to the Map you created on the previous step.

The code should look something as the one presented in onDraw method from How to correctly draw text in an extended class for TextView? , however you will set the previously determined typeface instead of applying super.getTypeface() .

Hope it helps you

Have you try with applying that all font to same textView's text one by one. I think with that you can apply the more more effect to same TextView.

Milos's code is right. In addition i have puted my own explaination. You can add your fonts in to the assets foldera and after that you can apply that font to the textView one-by-one.

Not sure but might be useful to you.

My Code:

Typeface font1 = Typeface.createFromAsset(getAssets(), "YOUR_FONT1.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "YOUR_FONT2.ttf");
Typeface font3 = Typeface.createFromAsset(getAssets(), "YOUR_FONT3.ttf");

chips_text.setTypeface(font1);
chips_text.setTypeface(font2);
chips_text.setTypeface(font3);

Feel free to comment and queries.

The best way right now for API 16+ is to define a font resource file if you are using Support Library above v26 or the new AndroidX libraries, basically you add your normal and italic ttf font files in the fonts folder and create a font resource xml and basically make it look something like

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
    app:fontStyle="normal"
    app:fontWeight="400"
    app:font="@font/custom_regular_font" />
<font
    app:fontStyle="italic"
    app:fontWeight="400"
    app:font="@font/custom_italic_font" />
<font
    app:fontStyle="normal"
    app:fontWeight="700"
    app:font="@font/custom_bold_font" />
</font-family>

the last one is for bold fonts, apply this xml suppose custom_font_family.xml to your textview as android:fontFamily="@font/custom_font_family", now any html text you set with fromHtml with any of the three span types will use the proper fonts, when needed, this allows you to even have a custom font family mixing entirely different fonts and doesn't quite literally have to be from the same family.

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