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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 Spannable 实现 TextView 的颜色渐变效果,你可以使用 ForegroundColorSpan 来设置不同部分的字体颜色,并根据需要调整它们的位置和颜色值。 以下是一个示例代码,演示如何使用 Spannable 实现颜色渐变效果: ```java TextView textView = findViewById(R.id.textView); String text = "Hello World!"; Spannable spannable = new SpannableString(text); // 定义渐变色数组 int[] colors = {Color.RED, Color.GREEN, Color.BLUE}; // 定义颜色变化位置数组 float[] positions = {0f, 0.5f, 1f}; for (int i = 0; i < text.length(); i++) { // 计算当前字符的颜色 int color = interpolateColor(colors, positions, (float) i / (text.length() - 1)); // 创建 ForegroundColorSpan,并设置字体颜色 ForegroundColorSpan span = new ForegroundColorSpan(color); // 设置 span 的起始位置和结束位置 spannable.setSpan(span, i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } // 将 spannable 设置给 TextView textView.setText(spannable); ``` 在这个示例中,我们首先创建了一个 SpannableString 对象,并将其初始化为需要处理的文本。然后,我们使用一个循环遍历文本中的每个字符,计算出当前字符的颜色值,然后创建一个 ForegroundColorSpan 对象,并将其应用到对应的字符上。最后,我们将处理后的 SpannableString 设置给 TextView。 在 interpolateColor 方法中,我们使用了一个插值算法来计算颜色的渐变值。你可以根据需要自定义这个方法,实现不同颜色渐变效果。 这样,TextView 的文本就会呈现出颜色渐变的效果。你可以根据实际需求和喜好,调整渐变色数组、颜色变化位置数组以及插值算法来实现不同颜色渐变效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值