Android - Combining multiple font styles into one font (Typeface)

I have several fonts with different styles, say: "MyFont_default.otf", "MyFont_italic.otf", "MyFont_bold.otf"

Typically, I would set the TextView font as follows:

Typeface tf=Typeface.createFromAsset(context.getAssets(), "MyFont_italic.otf"); textView.setTypeface(tf); 

And my question is: is it possible to combine all these fonts into one font, for example "MyFont", and depending on the TextView style defined in the XML layout file (italic, bold), TextView to display in the corresponding font?

+5
source share
2 answers

Use some kind of font editor to combine the fonts into one file. Many fonts come with several styles in one file, which is what you need in this case.

+1
source

I do not have the proper reputation for comment, but I can confirm that the approaches work. Using FontForge (binary for Windows):

  • Open multiple .otf files with FontForge (Regular, Bold, Italic).
  • Be sure to select a window that displays the "Regular" version of the font.
  • Use file> Save ttc from this window
  • See what other sub-backgrounds are listed in the save dialog (bold, italics)
  • Save and use this ttc file in Android

I tested it with the html text displayed in the TextView: <b>Test</b> Test <i>Test</i> → " Test Test Test ", which previously did not work in Android 2.3.7, and with this ttc font all three versions of the text are displayed perfectly.

As I said, it is important to choose the Regular font when exporting, because one Android uses it as "default" (first I exported from Bold, and everything was in bold).

+2
source

All Articles