We must use our own TypefaceSpan class.
SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "fonts/khmerbibleregular.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);
The custom TypefaceSpan class passes your Activity context and the font name in your Assets / fonts directory. It downloads the file and caches a new instance of Typeface in memory. The full implementation of TypefaceSpan is surprisingly simple:
public class TypefaceSpan extends MetricAffectingSpan {
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
Just copy the above class into your project and paste it into your code.
source
share