8

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?

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
Eli Konky
  • 2,697
  • 1
  • 20
  • 17

4 Answers4

1

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"

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265
  • 2
    Using single font file containing different styles (Regular, Bold, Italic etc.) is easy. Problem is with fonts where each style is in different file (i.e. font_regular.ttf, font_bold.ttf, font_italic.ttf). Using `setTypeface` can set only one Typeface, therefore only one style can be used. What [Eli Konky](http://stackoverflow.com/users/102092/eli-konky) and I need is to use several font files with single TextView. – suda Jan 29 '13 at 10:00
  • 1
    According to this: http://stackoverflow.com/questions/10191510/android-combining-multiple-font-styles-into-one-single-font-typeface and to the fact that setTypeface() will replace the default ttf file, used by android, by your custom ttf file: the only way (for the moment) to use different fonts for a single text view is to manually merge those fonts in one ttf file. This can be done by using a font editor (like http://sourceforge.net/projects/ttfedit/). Hope it helps. – Milos Cuculovic Jan 29 '13 at 10:29
  • 2
    I don't see option to merge two files in TTFEdit (or any other font editing software I've tested). Have to ask question about merging font files :) – suda Feb 03 '13 at 14:50
  • Here is an example: http://sourceforge.net/projects/ttfedit/ You can google TTF Editor – Milos Cuculovic Feb 04 '13 at 08:05
  • 2
    I couldn't see the example in that link, can you elaborate on how to merge the font files? – MonkeyBonkey Sep 14 '13 at 11:42
1

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.

0

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

Community
  • 1
  • 1
apenasVB
  • 1,463
  • 1
  • 11
  • 21
0

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.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • Yes I did. Unfortunately every call to setTypeface overwrites previous one. – suda Feb 04 '13 at 14:38
  • Ok then you have to follow with Milos's answer. You must have to create one font which include all the things that you want and then set it to the specify View. – Shreyash Mahajan Feb 05 '13 at 03:31