android - Use different typefaces for the same TextView with HTL formatting -
i know how specify custom typeface textview. of now, application using 1 custom font, splitted in 2 ttf files. 1 regular characters , toher 1 bold characters.
now, able use both inone textview html.fromhtml(). works system fonts, should able own typeface. currently, bold characters drawn regular font , fake bold text text paint quite ugly.
any idea ?
thanks
if want use multiple custom fonts within single textview:
use following code:(i'm using bangla , tamil font)
textview txt = (textview) findviewbyid(r.id.custom_fonts); txt.settextsize(30); typeface font = typeface.createfromasset(getassets(), "akshar.ttf"); typeface font2 = typeface.createfromasset(getassets(), "bangla.ttf"); spannablestringbuilder ss = new spannablestringbuilder("আমারநல்வரவு"); ss.setspan (new customtypefacespan("", font2), 0, 4,spanned.span_exclusive_inclusive); ss.setspan (new customtypefacespan("", font), 4, 11,spanned.span_exclusive_inclusive); txt.settext(ss); the outcome is:

customtypefacespan class:
package my.app; import android.graphics.paint; import android.graphics.typeface; import android.text.textpaint; import android.text.style.typefacespan; public class customtypefacespan extends typefacespan { private final typeface newtype; public customtypefacespan(string family, typeface type) { super(family); newtype = type; } @override public void updatedrawstate(textpaint ds) { applycustomtypeface(ds, newtype); } @override public void updatemeasurestate(textpaint paint) { applycustomtypeface(paint, newtype); } private static void applycustomtypeface(paint paint, typeface tf) { int oldstyle; typeface old = paint.gettypeface(); if (old == null) { oldstyle = 0; } else { oldstyle = old.getstyle(); } int fake = oldstyle & ~tf.getstyle(); if ((fake & typeface.bold) != 0) { paint.setfakeboldtext(true); } if ((fake & typeface.italic) != 0) { paint.settextskewx(-0.25f); } paint.settypeface(tf); } }
Comments
Post a Comment