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

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

标签:Androidjava


咱们常常会遇到一种情景,就是一行文字,却有不一样的字体或不一样的颜色,好比这样:
android

通常来讲,有三种方案来实现:多个TextView、使用Html标签、使用Spannableapp

这里,Html标签的方式在不一样字体时很差处理,能够忽略。而多个TextView的方式呢,会增长视图的个数,有点low。字体

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

示例代码

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;
        }
    }
}
相关文章
相关标签/搜索