Android设置TextView高亮字体、字体颜色渐变
渐变效果图:
实现的工具类TextFontUtils
:
public class TextFontUtils {
public static String HIGHLIGHT_COLOR = "#0fc264";
public static void setHighlightFont(String regexStr, String targetStr, TextView textView) {
targetStr = targetStr.replaceAll(regexStr, "<font color='" + HIGHLIGHT_COLOR + "'>" + regexStr + "</font>");
textView.setText(Html.fromHtml(targetStr));
}
public static void setGradientFont(TextView textView, String startColor, String endColor){
LinearGradient gradient = new LinearGradient(0, 0, 0,
textView.getPaint().getTextSize(),
Color.parseColor(startColor), Color.parseColor(endColor),
Shader.TileMode.CLAMP);
textView.getPaint().setShader(gradient);
textView.invalidate();
}
}