使TextView部分字体变色

开发中可能有让文本中部分关键字体变色的需求,例如:搜索列表展示时,搜索关键字特别显示

有好几种方式,在此介绍比较好用的一种,Android提供了 SpannableStringBuilder

1、AbsoluteSizeSpan(int size) —— 设置字体大小,参数是绝对数值,相当于Word中的字体大小

2、BackgroundColorSpan(int color) —— 背景着色,参数是颜色数值,可以直接使用android.graphics.Color里面定义的常量,或是用Color.rgb(int, int, int)

3、ForegroundColorSpan(int color) —— 前景着色,也就是字的着色,参数与背景着色一致

4、TypefaceSpan(String family) —— 字体,参数是字体的名字比如“sans”, “sans-serif”等

5、StyleSpan(Typeface style) —— 字体风格,比如粗体,斜体,参数是android.graphics.Typeface里面定义的常量,如Typeface.BOLD,Typeface.ITALIC等等。StrikethroughSpan—-如果设置了此风格,会有一条线从中间穿过所有的字,就像被划掉一样

6、RelativeSizeSpan(float proportion) —— 设置字体大小,参数是相对于默认字体大小的倍数,比如默认字体大小是x, 那么设置后的字体大小就是x*proportion,这个用起来比较灵活,proportion>1就是放大(zoom in), proportion<1就是缩小(zoom out)

7、ScaleXSpan(float proportion) —— 缩放字体,与上面的类似,默认为1,设置后就是原来的乘以proportion,大于1时放大(zoon in),小于时缩小(zoom out)

代码:

String str = "碧桂园西湖";//TextView所有字体
int fstart = str.indexOf("西湖");//文本中的关键字
int fend = fstart+"西湖".length();
SpannableStringBuilder style=new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
the_text = (TextView) findViewById(R.id.the_text);
the_text.setText(style);
显示结果为:碧桂园 西湖

为了方便使用可以做一下封装:

封装了两种方式,大家可以任选一种:

1、

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

/**
 * Created by qiyong on 2018/1/30.
 *
 * 工具类:用于显示TextView中关键字的颜色
 */
public class SearchTextShow {

    private TextView theText;
    private String allString;//所有字符串
    private String showStriing;//需要特殊颜色显示的字符串
    private int theColor;//颜色

    public SearchTextShow(TextView theText, String allString, String showStriing, int theColor) {
        this.theText = theText;
        this.allString = allString;
        this.showStriing = showStriing;
        this.theColor = theColor;
        showTheText();
    }
    public void showTheText(){
        int theStart = allString.indexOf(showStriing);
        int theEnd = theStart+showStriing.length();
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(allString);
        spannableStringBuilder.setSpan(new ForegroundColorSpan(theColor),theStart,theEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        theText.setText(spannableStringBuilder);
    }
}

2、

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;

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

/**
 * Created by qiyong on 2018/1/30.
 *
 * 自定义TextView 文本中关键字的颜色特殊标识
 */
public class MyTextView extends TextView{

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setSpecifiedTextsColor(String text, String specifiedTexts, int color) {
        List<Integer> sTextsStartList = new ArrayList<>();

        int sTextLength = specifiedTexts.length();
        String temp = text;
        int lengthFront = 0;//记录被找出后前面的字段的长度
        int start = -1;
        do {
            start = temp.indexOf(specifiedTexts);

            if (start != -1) {
                start = start + lengthFront;
                sTextsStartList.add(start);
                lengthFront = start + sTextLength;
                temp = text.substring(lengthFront);
            }

        } while (start != -1);

        SpannableStringBuilder styledText = new SpannableStringBuilder(text);
        for (Integer i : sTextsStartList) {
            styledText.setSpan(
                    new ForegroundColorSpan(color),
                    i,
                    i + sTextLength,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        setText(styledText);
    }
}
the_text.setSpecifiedTextsColor("锦艺国际华都", "锦艺",Color.parseColor("#FF0000"));

猜你喜欢

转载自blog.csdn.net/qiy6010/article/details/79213255