我正在尝试在我的应用程序中使用Custom Typeface Span,但它不起作用。这是我的代码
Typeface font3 = Typeface.createFromAsset(context.getAssets(), "fontawesome-webfont.ttf");
SpannableStringBuilder SS = new SpannableStringBuilder("");
SS.setSpan(new CustomTypefaceSpan("", font3), 0, 8,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(SS);
这里我尝试在自定义字体跨度中使用Font-Awesome。
答案 0 :(得分:0)
您可以创建一个Custom TypefaceSpan,然后可以在任何地方使用它来在任何地方应用字体样式。 如下所述创建一个类TypefaceSpan。
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link android.text.Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), typefaceName);//String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
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);
}
,然后您可以使用此类来应用字体样式。 示例-
actionBar=getSupportActionBar();
SpannableString s = new SpannableString("Hostel-16");
s.setSpan(new com.example.suraj.hostel16.TypefaceSpan(getApplicationContext(),"georgia.ttf"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);