2 qlx2522 qlx2522 于 2015.06.02 11:42 提问

android SpannableStringBuilder设置自定义字体

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

1个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.02 12:12
已采纳
读取
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);
}
}
qlx2522
qlx2522 谢谢你的回答。是我没问清楚,我是想解析html 然后用StringBuilder 承载,最后显示在textView 这里用到得自定义字体。虽然不能完整的解决我的问题,但是还是谢谢你。已经找到解决办法
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!