Android TextView设置多彩文字

时间:2023-03-09 00:42:26
Android TextView设置多彩文字

在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求;

以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心;

直到接触到了SpannableStringBuilder,感觉整个人都好了;

在我搭建界面布局,会有一些带String占位符的默认文字,如:"现在的气温是:%s","今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s.";

之后在获取到数据时,直接String.format(String target, String... data),就能在对应位置插入数据;

最近遇到一个插入的数据还要换成另一种颜色的需求,觉得这个需求应该比较常见,所以就写了个工具类;

 /**
* TextView色彩工具类
* Created by me on 2015-08-10.
*/
public class TextViewColorUtil { /**
* 在文字内容为"xxxxx%sxxxx"(一个格式化占位符)或"xxxx%1$sxxxx%2$x......xxxx%n$sxxxx"时(多个格式化占位符),完成格式化同时,设置新加入的文字颜色,同时也能够设置原来文字的颜色;
* <p/>
* 注:请务必保证单个格式化时,使用%s占位符;多格式化时,使用%n$s占位符;
* 占位符数必须和想填入的字符串数目一致;
*
* @param texts 如果可变参数长度为0,不做处理;如果文字长度为0,默认为""
* @param defaultColorId R.color.xxx 如果不想改变默认颜色(冒号前的文字颜色),可以填null
* @param newContentColorId R.color.xxx
*/
public static void setSubColorText(Context context, TextView tv, Integer defaultColorId, int newContentColorId, String... texts) { if (texts != null) {
if (texts.length == 1) {//单格式化参数情况
if (defaultColorId != null)//1.如果有设置改编默认文字颜色,给予改变
tv.setTextColor(defaultColorId); String text = texts[0];
if (StringUtil.isEmpty(text))//2.如果文字内容为null或者长度0,默认其为""
text = ""; String originText = tv.getText().toString();//3.格式化,记录添加的字符串的起止index
int indexStart = originText.indexOf("%s");
int indexEnd = indexStart + text.length();
String foo = String.format(originText, text);
tv.setText(foo); if (indexEnd > indexStart) {//4.如果有必要换色,执行
SpannableStringBuilder style = new SpannableStringBuilder(foo);
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(newContentColorId)), indexStart, indexEnd, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.setText(style);
} } else if (texts.length > 1) {//多格式化
if (defaultColorId != null)//1.如果有设置改编默认文字颜色,给予改变
tv.setTextColor(defaultColorId); int[] indexesStart = new int[texts.length];
int[] indexesEnd = new int[texts.length];
String originText = tv.getText().toString(); for (int i = 0; i < texts.length; i++) {
String text = texts[i];
if (StringUtil.isEmpty(text)) {//2.如果文字内容为null或者长度0,默认其为""
text = "";
} String regular = "%" + (i + 1) + "$s";//3.格式化,记录添加的字符串的起止index
indexesStart[i] = originText.indexOf(regular);
if (i > 0) {
int indexFix = 0;
for (int j = 0; j <= i - 1; j++) {
String formerRegular = "%" + (j + 1) + "$s";
indexFix += (indexesEnd[j] - indexesStart[j]) - formerRegular.length();
}
indexesStart[i] += indexFix;
}
indexesEnd[i] = indexesStart[i] + text.length();
}
String foo = String.format(originText, (Object[]) texts);
tv.setText(foo);
SpannableStringBuilder style = new SpannableStringBuilder(foo);
for (int i = 0; i < texts.length; i++) {
if (indexesEnd[i] > indexesStart[i])//4.如果有必要换色,执行
style.setSpan(new ForegroundColorSpan(context.getResources().getColor(newContentColorId)), indexesStart[i], indexesEnd[i], Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
tv.setText(style);
}
}
}
}

Code

目前只有一种颜色,如果有每个插入的数据还要不同色彩的需求...也蛮好改的.

把传入的参数int newContentColorId换成int[] newContentColorIds然后稍微改改逻辑就ok啦.

>_>

就以之前的两个例子举例吧:

1.

 textView.setText("现在的气温是:%s");
TextViewColorUtil.setSubColorText(MainActivity.this, textView, null, R.color.blue,"3°c");

"现在的气温是:%s"

2.

 tv.setText("今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s.");
TextViewColorUtil.setSubColorText(MainActivity.this, tv, null, R.color.red, "晴", "22°c", "9°c", "47%", "19");

"今天天气:%1$s,最高气温:%2$s,最低气温:%3$s,降雨率:%4$s,pm2.5:%5$s."