Android SpannableString - 让你的文本变
2020-03-24 本文已影响0人
古舟咕咕
本文章为原创内容,未经允许,禁止转载。
我们通常都会在 TextView 上面显示一些文字,可是这些文字的样式呢,都非常单一。如果我们想在一个 TextView 上显示多种样式的文本该怎么办呢?这时候就轮到 SpannableString 上场了。对这个东西不熟悉的小伙伴,可以先去别的地方了解一下,这里就不多介绍了。
于是我就基于 SpannableString 写了一个工具类,可以让你轻轻松松在 TextView 上显示多姿多彩的文本。目前写出来的代码,可以动态设置大小,颜色,样式,背景,下划线和删除线。
注:SpannableString 这个东西花样实在是太多,因此只选择了其中的部分样式,剩下的就以后再补吧。

1.使用方法
使用方法非常简单,看下面的代码:
String str="小王、小李和小刘是好朋友\n他们都认识同学小张\n有一天小李认识了新朋友\n她的名字叫做小红\n小红也认识了小王\n并且和小王成为了好朋友\n随后小张也认识了小红\n并且向小刘介绍了她\n于是他就再也没有理过小王";
TextView tv=new TextView(this);
tv.setText(new EasyStringUtils(this, str)
.setSize(20, "小")// 所有的"小"设为20sp
.setColor(0xffff0000, new String[]{"好","友"})// 所有的"好"和"友"设为红色
.setLineStyle(Typeface.BOLD, 2)// 第2行加粗
.setUnderline()// 所有内容添加下划线
.setLineStrikethrough(new int[]{6,8})// 第6行和第8行添加删除线
.setLineBackground(0x200000ff, EasyStringUtils.TYPE_CONTAINS, new String[]{"小刘","朋友"})// 所有含有"小刘"和"朋友"的行设为淡蓝色背景
.getStr());
简单来说,就是传入一个 String,可以设置一些样式后再使用 getStr() 方法获取到那个设置好的 SpannableString。我们的 TextView 可以直接把这个东西给设置上,然后你看到的就是上面的效果啦。怎么样,是不是很简单呢?
2.完整代码
当然啦,在使用之前,你肯定得有下面的代码,要不然,我们的系统是不是又该抱怨:‘“EasyStringUtils 是什么东东啊?”
嗯,下面的代码虽然还不够全面,但是也有足足1000行呢。所有的函数及其作用我都注释得明明白白的,生怕你看不明白。如果你不想看,就直接 Ctrl+C、Ctrl+V 吧,哈哈。
EasyStringUtils.java
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan;
/**
* 多功能字符串工具类
* @author: 木鱼舟
* @version: 1.0
*/
public class EasyStringUtils {
/** 类型 包含某字符串 */
public static final int TYPE_CONTAINS=0;
/** 类型 以某字符串开始 */
public static final int TYPE_STARTS_WITH=-1;
/** 类型 以某字符串结束 */
public static final int TYPE_ENDS_WITH=-2;
private static Context mContext;// 上下文对象
private static String mStr;// 传入的字符串
private static SpannableString mSpannableStr;// 可扩展的字符串对象
/**
* 构造方法
* @param context: 上下文对象
* @param str: 传入的字符串
*/
public EasyStringUtils(Context context, String str) {
mContext = context;
mStr = str;
mSpannableStr = new SpannableString(str);
}
/**
* 设置字体大小
* @param sp: 字体大小 (以sp为单位)
* @return this: 返回当前类的实例
*/
public EasyStringUtils setSize(int sp) {
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, 0, mStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/**
* 设置所有指定的字符串的字体大小
* @param sp: 字体大小 (以sp为单位)
* @param str: 指定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setSize(int sp, String str) {
StringBuffer sb=new StringBuffer(mStr);
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(str, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(str, indexCount) + str.length();
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
return this;
}
/**
* 设置所有指定的多个字符串的字体大小
* @param sp: 字体大小 (以sp为单位)
* @param strs: 指定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setSize(int sp, String[] strs) {
StringBuffer sb=new StringBuffer(mStr);
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
for (String s:strs) {
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(s, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(s, indexCount) + s.length();
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
}
return this;
}
/**
* 设置指定的单行字体大小
* @param sp: 字体大小 (以sp为单位)
* @param line: 指定的行
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineSize(int sp, int line) {
StringBuffer sb=new StringBuffer(mStr + "\n");
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
int indexCount=0;
for (int i=1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == line) {
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 设置指定的多行字体大小
* @param sp: 字体大小 (以sp为单位)
* @param lines: 指定的行数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineSize(int sp, int[] lines) {
StringBuffer sb=new StringBuffer(mStr + "\n");
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
for (int currentLine:lines) {
int indexCount=0;
for (int i = 1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == currentLine) {
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 指定一个类型并设置给定的字符串所在行的字体大小
* @param sp: 字体大小 (以sp为单位)
* @param type: 指定的类型
* @param str: 给定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineSize(int sp, int type, String str) {
StringBuffer sb=new StringBuffer(mStr + "\n");
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, str);
if (condition) {
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 指定一个类型并设置给定的多个字符串所在行的字体大小
* @param sp: 字体大小 (以sp为单位)
* @param type: 指定的类型
* @param strs: 给定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineSize(int sp, int type, String[] strs) {
StringBuffer sb=new StringBuffer(mStr + "\n");
float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
for (String s:strs) {
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, s);
if (condition) {
AbsoluteSizeSpan absoluteSizeSpan=new AbsoluteSizeSpan((int)(sp * fontScale + 0.5f));
mSpannableStr.setSpan(absoluteSizeSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 设置字体颜色
* @param color: 字体颜色
* @return this: 返回当前类的实例
*/
public EasyStringUtils setColor(int color) {
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, 0, mStr.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/**
* 设置所有指定的字符串的字体颜色
* @param color: 字体颜色
* @param str: 指定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setColor(int color, String str) {
StringBuffer sb=new StringBuffer(mStr);
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(str, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(str, indexCount) + str.length();
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
return this;
}
/**
* 设置所有指定的多个字符串的字体颜色
* @param color: 字体颜色
* @param strs: 指定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setColor(int color, String[] strs) {
StringBuffer sb=new StringBuffer(mStr);
for (String s:strs) {
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(s, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(s, indexCount) + s.length();
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
}
return this;
}
/**
* 设置指定的单行字体颜色
* @param color: 字体颜色
* @param line: 指定的行
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineColor(int color, int line) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i=1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == line) {
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 设置指定的多行字体颜色
* @param color: 字体颜色
* @param lines: 指定的行数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineColor(int color, int[] lines) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (int currentLine:lines) {
int indexCount=0;
for (int i = 1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == currentLine) {
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 指定一个类型并设置给定的字符串所在行的字体颜色
* @param color: 字体颜色
* @param type: 指定的类型
* @param str: 给定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineColor(int color, int type, String str) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, str);
if (condition) {
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 指定一个类型并设置给定的多个字符串所在行的字体颜色
* @param color: 字体颜色
* @param type: 指定的类型
* @param strs: 给定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineColor(int color, int type, String[] strs) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (String s:strs) {
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, s);
if (condition) {
ForegroundColorSpan foregroundColorSpan=new ForegroundColorSpan(color);
mSpannableStr.setSpan(foregroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 设置字体风格
* @param style: 字体风格
* @return this: 返回当前类的实例
*/
public EasyStringUtils setStyle(int style) {
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, 0, mStr.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/**
* 设置所有指定的字符串的字体风格
* @param style: 字体风格
* @param str: 指定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setStyle(int style, String str) {
StringBuffer sb=new StringBuffer(mStr);
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(str, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(str, indexCount) + str.length();
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
return this;
}
/**
* 设置所有指定的多个字符串的字体风格
* @param type: 字体风格
* @param strs: 指定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setStyle(int style, String[] strs) {
StringBuffer sb=new StringBuffer(mStr);
for (String s:strs) {
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(s, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(s, indexCount) + s.length();
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
}
return this;
}
/**
* 设置指定的单行字体风格
* @param type: 字体风格
* @param line: 指定的行
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStyle(int style, int line) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i=1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == line) {
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, indexCount, index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 设置指定的多行字体风格
* @param type: 字体风格
* @param lines: 指定的行数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStyle(int style, int[] lines) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (int currentLine:lines) {
int indexCount=0;
for (int i = 1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == currentLine) {
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, indexCount, index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 指定一个类型并设置给定的字符串所在行的字体风格
* @param style: 字体风格
* @param type: 指定的类型
* @param str: 给定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStyle(int style, int type, String str) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, str);
if (condition) {
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, indexCount, index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 指定一个类型并设置给定的多个字符串所在行的字体风格
* @param style: 字体风格
* @param type: 指定的类型
* @param strs: 给定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStyle(int style, int type, String[] strs) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (String s:strs) {
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, s);
if (condition) {
StyleSpan styleSpan=new StyleSpan(style);
mSpannableStr.setSpan(styleSpan, indexCount, index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 设置背景颜色
* @param color: 背景颜色
* @return this: 返回当前类的实例
*/
public EasyStringUtils setBackground(int color) {
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, 0, mStr.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/**
* 设置所有指定的字符串的背景颜色
* @param color: 背景颜色
* @param str: 指定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setBackground(int color, String str) {
StringBuffer sb=new StringBuffer(mStr);
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(str, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(str, indexCount) + str.length();
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
return this;
}
/**
* 设置所有指定的多个字符串的背景颜色
* @param color: 背景颜色
* @param strs: 指定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setBackground(int color, String[] strs) {
StringBuffer sb=new StringBuffer(mStr);
for (String s:strs) {
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(s, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(s, indexCount) + s.length();
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
}
return this;
}
/**
* 设置指定的单行背景颜色
* @param color: 背景颜色
* @param line: 指定的行
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineBackground(int color, int line) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i=1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == line) {
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 设置指定的多行背景颜色
* @param color: 背景颜色
* @param lines: 指定的行数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineBackground(int color, int[] lines) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (int currentLine:lines) {
int indexCount=0;
for (int i = 1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == currentLine) {
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 指定一个类型并设置给定的字符串所在行的背景颜色
* @param color: 背景颜色
* @param type: 指定的类型
* @param str: 给定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineBackground(int color, int type, String str) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, str);
if (condition) {
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 指定一个类型并设置给定的多个字符串所在行的背景颜色
* @param color: 背景颜色
* @param type: 指定的类型
* @param strs: 给定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineBackground(int color, int type, String[] strs) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (String s:strs) {
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, s);
if (condition) {
BackgroundColorSpan backgroundColorSpan=new BackgroundColorSpan(color);
mSpannableStr.setSpan(backgroundColorSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 设置下划线
* @return this: 返回当前类的实例
*/
public EasyStringUtils setUnderline() {
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, 0, mStr.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/**
* 设置所有指定的字符串的下划线
* @param str: 指定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setUnderline(String str) {
StringBuffer sb=new StringBuffer(mStr);
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(str, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(str, indexCount) + str.length();
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
return this;
}
/**
* 设置所有指定的多个字符串的下划线
* @param strs: 指定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setUnderline(String[] strs) {
StringBuffer sb=new StringBuffer(mStr);
for (String s:strs) {
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(s, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(s, indexCount) + s.length();
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
}
return this;
}
/**
* 设置指定的单行下划线
* @param line: 指定的行
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineUnderline(int line) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i=1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == line) {
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 设置指定的多行下划线
* @param lines: 指定的行数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineUnderline(int[] lines) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (int currentLine:lines) {
int indexCount=0;
for (int i = 1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == currentLine) {
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 指定一个类型并设置给定的字符串所在行的下划线
* @param type: 指定的类型
* @param str: 给定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineUnderline(int type, String str) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, str);
if (condition) {
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 指定一个类型并设置给定的多个字符串所在行的下划线
* @param type: 指定的类型
* @param strs: 给定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineUnderline(int type, String[] strs) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (String s:strs) {
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, s);
if (condition) {
UnderlineSpan underlineSpan=new UnderlineSpan();
mSpannableStr.setSpan(underlineSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 设置删除线
* @return this: 返回当前类的实例
*/
public EasyStringUtils setStrikethrough() {
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, 0, mStr.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return this;
}
/**
* 设置所有指定的字符串的删除线
* @param str: 指定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setStrikethrough(String str) {
StringBuffer sb=new StringBuffer(mStr);
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(str, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(str, indexCount) + str.length();
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
return this;
}
/**
* 设置所有指定的多个字符串的删除线
* @param strs: 指定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setStrikethrough(String[] strs) {
StringBuffer sb=new StringBuffer(mStr);
for (String s:strs) {
int indexCount=0;
while (true) {
int indexStart=sb.indexOf(s, indexCount);
if (indexStart == -1) {
break;
}
int indexEnd=sb.indexOf(s, indexCount) + s.length();
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, indexStart, indexEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
indexCount = indexEnd;
}
}
return this;
}
/**
* 设置指定的单行删除线
* @param line: 指定的行
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStrikethrough(int line) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i=1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == line) {
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 设置指定的多行删除线
* @param lines: 指定的行数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStrikethrough(int[] lines) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (int currentLine:lines) {
int indexCount=0;
for (int i = 1;;i++) {
int index=sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
if (i == currentLine) {
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 指定一个类型并设置给定的字符串所在行的删除线
* @param type: 指定的类型
* @param str: 给定的字符串
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStrikethrough(int type, String str) {
StringBuffer sb=new StringBuffer(mStr + "\n");
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, str);
if (condition) {
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
return this;
}
/**
* 指定一个类型并设置给定的多个字符串所在行的删除线
* @param type: 指定的类型
* @param strs: 给定的字符串数组
* @return this: 返回当前类的实例
*/
public EasyStringUtils setLineStrikethrough(int type, String[] strs) {
StringBuffer sb=new StringBuffer(mStr + "\n");
for (String s:strs) {
int indexCount=0;
for (int i = 1;;i++) {
int index = sb.indexOf("\n", indexCount);
if (index == -1) {
break;
}
boolean condition=getCondition(type, sb, indexCount, index, s);
if (condition) {
StrikethroughSpan strikethroughSpan=new StrikethroughSpan();
mSpannableStr.setSpan(strikethroughSpan, indexCount , index,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
indexCount = index + "\n".length();
}
}
return this;
}
/**
* 获取当前的可扩展字符串对象
* @return mSpannableStr: 返回可扩展字符串对象
*/
public SpannableString getStr() {
return mSpannableStr;
}
/**
* 根据所给类型获取条件是否满足
* @param type: 所给类型
* @param sb: StringBuffer
* @param start: 开始索引
* @param end: 结束索引
* @param str: 给定的字符串
* @return condition: 返回条件是否满足
*/
private boolean getCondition(int type, StringBuffer sb, int start, int end, String str) {
boolean condition=false;
switch (type) {
case TYPE_CONTAINS:
condition = sb.substring(start, end).contains(str);
break;
case TYPE_STARTS_WITH:
condition = sb.substring(start, end).startsWith(str);
break;
case TYPE_ENDS_WITH:
condition = sb.substring(start, end).endsWith(str);
break;
default:
break;
}
return condition;
}
}