package com.yeshi.ec.rebate.myapplication.util;
|
|
import android.content.Context;
|
import android.graphics.Color;
|
import android.support.v4.content.ContextCompat;
|
import android.text.Html;
|
import android.text.Spannable;
|
import android.text.SpannableString;
|
import android.text.SpannableStringBuilder;
|
import android.text.Spanned;
|
import android.text.TextUtils;
|
import android.text.style.CharacterStyle;
|
import android.text.style.ForegroundColorSpan;
|
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
/**
|
* 文字变色工具类
|
*/
|
public class wordUtil {
|
|
/**
|
* 关键字高亮变色
|
*
|
* @param context 上下文
|
* @param color 变化的色值
|
* @param text 文字
|
* @param keyword 文字中的关键字
|
* @return 结果SpannableString
|
*/
|
public static SpannableString matcherSearchTitle(Context context, int color, String text, String keyword) {
|
SpannableString s = new SpannableString(text);
|
keyword = escapeExprSpecialWord(keyword);
|
text = escapeExprSpecialWord(text);
|
if (text.contains(keyword) && !TextUtils.isEmpty(keyword)) {
|
try {
|
Pattern p = Pattern.compile(keyword);
|
Matcher m = p.matcher(s);
|
while (m.find()) {
|
int start = m.start();
|
int end = m.end();
|
//注意:请使用ContextCompat.getColor获取颜色值。
|
s.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
}
|
} catch (Exception e) {
|
// Log.e("错误:", e.toString());
|
}
|
}
|
return s;
|
}
|
|
/**
|
* 关键字高亮显示
|
*
|
* @param text 文字
|
* @param keyword1 文字中的关键字数组
|
* @return
|
*/
|
public static SpannableStringBuilder matcherSearchContent(String text, String[] keyword1) {
|
String[] keyword = new String[keyword1.length];
|
System.arraycopy(keyword1, 0, keyword, 0, keyword1.length);
|
SpannableStringBuilder spannable = new SpannableStringBuilder(text);
|
|
CharacterStyle span;
|
String wordReg;
|
for (int i = 0; i < keyword.length; i++) {
|
String key = "";
|
// 处理通配符问题
|
if (keyword[i].contains("*") || keyword[i].contains("(") || keyword[i].contains(")")) {
|
char[] chars = keyword[i].toCharArray();
|
for (int k = 0; k < chars.length; k++) {
|
if (chars[k] == '*' || chars[k] == '(' || chars[k] == ')') {
|
key = key + "\\" + String.valueOf(chars[k]);
|
} else {
|
key = key + String.valueOf(chars[k]);
|
}
|
}
|
keyword[i] = key;
|
}
|
|
wordReg = "(?i)" + keyword[i]; //忽略字母大小写
|
Pattern pattern = Pattern.compile(wordReg);
|
Matcher matcher = pattern.matcher(text);
|
while (matcher.find()) {
|
span = new ForegroundColorSpan(Color.parseColor("#ff666666"));
|
spannable.setSpan(span, matcher.start(), matcher.end(), Spannable.SPAN_MARK_MARK);
|
}
|
}
|
|
return spannable;
|
}
|
|
|
/**
|
* 转义正则特殊字符 ($()*+.[]?\^{},|)
|
*
|
* @param keyword
|
* @return keyword
|
*/
|
public static String escapeExprSpecialWord(String keyword) {
|
if (!TextUtils.isEmpty(keyword)) {
|
String[] fbsArr = {"\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|"};
|
for (String key : fbsArr) {
|
if (keyword.contains(key)) {
|
keyword = keyword.replace(key, "\\" + key);
|
}
|
}
|
}
|
return keyword;
|
}
|
|
/**
|
* 通过Html.fromHtml方式修改颜色(解决SDK版本问题)
|
*
|
* @param html
|
* @return
|
*/
|
public static Spanned fromHtml(String html) {
|
Spanned result;
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
|
} else {
|
result = Html.fromHtml(html);
|
}
|
return result;
|
}
|
|
/**
|
* 字体颜色修改
|
*
|
* @param content
|
* @param color
|
* @return
|
*/
|
public static String color(String content, int color) {
|
return "<font color=\"#" + Integer.toHexString(color) + "\" >" + content + "</font>";
|
}
|
|
/**
|
* 加粗字体
|
*
|
* @param content
|
* @return
|
*/
|
public static String bold(String content) {
|
return "<b>" + content + "</b>";
|
}
|
|
}
|