spannable加粗方式 :
StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
editable.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
如果要更新stylespan 需要調(diào)用 stylespan的 updateMeasureState 方法 而這個方法會調(diào)用stylespan的私有方法
private static void apply(Paint paint, int style) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int want = oldStyle | style;
Typeface tf;
if (old == null) {
tf = Typeface.defaultFromStyle(want);
} else {
tf = Typeface.create(old, want);
}
int fake = want & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
看這個方法可知 當(dāng)穿入的style為0時,是無法改變當(dāng)前span樣式的 ,所以,如果需要取消加粗,可以重寫stylespan的
updateDrawState方法 如下 :
@Override
public void updateDrawState(TextPaint ds) {
Typeface typeface = ds.getTypeface();
ds.setFakeBoldText(false);
ds.setTextSkewX(0);
if (typeface!=null){
ds.setTypeface(Typeface.create(typeface,Typeface.NORMAL));
}else {
ds.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
}
}