Spannable辅助类:实现不同字体、颜色

Spannable辅助类:实现不同字体、颜色

标签:Android


我们经常会遇到一种情景,就是一行文字,却有不同的字体或不同的颜色,比如这样:

一般来说,有三种方案来实现:多个TextView、使用Html标签、使用Spannable

这里,Html标签的方式在不同字体时不好处理,可以忽略。而多个TextView的方式呢,会增加视图的个数,有点low。

但是,Spannable这个玩意呢,虽然看起来无所不能,但是使用起来真是麻烦的一逼,文本的下标一不小心搞错了就会出错。所以呢,我对Spannable做了一下简单的封装,方便快速使用。目前仅支持同一个TextView显示不同字体、不同颜色。

示例代码

tvTitle.setText(SpannableBuilder.create(this)
        .append("关联店铺", R.dimen.sp16, R.color.text_33)
        .append("(请添加您的所有店铺)", R.dimen.sp12, R.color.text_99)
        .build());

辅助类

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:余天然 on 2017/2/22 下午4:06
 */
public class SpannableBuilder {

    private Context context;
    private List<SpanWrapper> list;

    private SpannableBuilder(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public SpannableBuilder append(String text, int textSize, int textColor) {
        list.add(new SpanWrapper(text, textSize, textColor));
        return this;
    }

    public Spannable build() {
        SpannableStringBuilder textSpan = new SpannableStringBuilder();

        int start = 0;
        int end = 0;

        for (int i = 0; i < list.size(); i++) {
            SpanWrapper wrapper = list.get(i);
            String text = wrapper.getText();
            start = end;
            end = end + text.length();
            textSpan.append(text);

            AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(getContext().getResources().getDimensionPixelSize(wrapper.getTextSize()));
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(getContext().getResources().getColor(wrapper.getTextColor()));

            textSpan.setSpan(sizeSpan, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            textSpan.setSpan(colorSpan, start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        }
        return textSpan;
    }

    public static SpannableBuilder create(Context context) {
        return new SpannableBuilder(context);
    }

    public Context getContext() {
        return context;
    }

    private class SpanWrapper {
        String text;
        int textSize;
        int textColor;

        public SpanWrapper(String text, int textSize, int textColor) {
            this.text = text;
            this.textSize = textSize;
            this.textColor = textColor;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getTextSize() {
            return textSize;
        }

        public void setTextSize(int textSize) {
            this.textSize = textSize;
        }

        public int getTextColor() {
            return textColor;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }
    }
}
原文地址:https://www.cnblogs.com/yutianran/p/9921498.html