开发者社区> 问答> 正文

关于android SpannableStringBuilder设置自定义字体

SpannableStringBuilder 怎么设置自定义 字体。
现在是这样
`setSpan(new TypefaceSpan(f.mFace), where, len,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);`
不知道 怎么读取assets文件中得字体 来设置

展开
收起
爵霸 2016-07-29 17:23:55 4007 0
1 条回答
写回答
取消 提交回答
  • 读取
    `
    Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/YourCustomFont.ttf");
    `
    使用:

     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);
    }
    }
    2019-07-17 20:01:57
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载