android spannable取消下划线,斜体,加粗

spannable加粗方式 :

       StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
        editable.setSpan(styleSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

如果要更新stylespan 需要调用 stylespan的 updateMeasureState 方法 而这个方法会调用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);
}

看这个方法可知 当穿入的style为0时,是无法改变当前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));
    }
}

你可能感兴趣的:(android spannable取消下划线,斜体,加粗)