This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

Projects often encountered, according to the need of keywords and special color value whether search content or keyword display list, for a particular word or phrase highlighted is one of the most common needs, provide Android didn’t bring such tools or components, but we can achieve our such a utility class, use the local direct call.

Text highlighted

Text highlighting is actually a special color for a character. Here are some common ways to do this

Highlight by loading Html tags

Android TextView can load paragraphs with Html tags by:

textView.setText(Html.fromHtml("<font color='red' size='24'>Hello World</font>"));

Copy the code

To highlight keywords, you can do this by combining keywords that need to be highlighted, for example:

Textview.settext (" This is my first Android project "+ html.fromhtml ("<font color='red' size='24'>Hello World</font>"));Copy the code

Text highlighting via SpannableString

So let’s take a quick look at SpannableString

SpannableString basic usage code example:

Private void addUrlSpan() {SpannableString spanString = new SpannableString(" hyperlink "); URLSpan span = new URLSpan("tel:0123456789"); spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.append(spanString); } private void addBackColorSpan() {SpannableString spanString = new SpannableString(" Text background color "); BackgroundColorSpan span = new BackgroundColorSpan(Color.YELLOW); spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.append(spanString); } private void addForeColorSpan() {SpannableString spanString = new SpannableString(" Text foreground color "); ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE); spanString.setSpan(span, 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.append(spanString); } // Set the font size private void addFontSpan() {SpannableString spanString = new SpannableString("36 "); AbsoluteSizeSpan span = new AbsoluteSizeSpan(36); spanString.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tv.append(spanString); }Copy the code

The above is more commonly used, there are other Settings such as font bold, underline, strikeout line, etc., can be achieved

Here we mainly used to set the background color of the font, through the regular expression to match the keyword, set the keyword matching paragraph highlight

/*** * Specifies the keyword highlighting string as a whole * @param originString Original string * @param keyWords highlighting string * @param highLightColor highLightColor value * @return Highlighted string  */ public static SpannableString getHighLightWord(String originString, String keyWords, int highLightColor) { SpannableString originSpannableString = new SpannableString(originString); if (! TextUtils.isEmpty(keyWords)) { Pattern pattern = Pattern.compile(keyWords); Matcher matcher = pattern.matcher(originSpannableString); while (matcher.find()) { int startIndex = matcher.start(); int endIndex = matcher.end(); originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } return originSpannableString; }Copy the code

In the extension, can support keywords, keywords split display

Similar: Test 1234 test 1234 test (tests are highlighted words, implement test/test/test highlighting respectively)

* @param originString * @param keyWords * @param highLightColor * @return */ public static SpannableString getHighLightWords(String originString, String keyWords, int highLightColor) { SpannableString originSpannableString = new SpannableString(originString); if (! TextUtils.isEmpty(keyWords)) { for (int i = 0; i < keyWords.length(); i++) { Pattern p = Pattern.compile(String.valueOf(keyWords.charAt(i))); Matcher m = p.matcher(originSpannableString); while (m.find()) { int start = m.start(); int end = m.end(); originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return originSpannableString; }Copy the code

Characters are ok, what about arrays? Is that ok?

/*** * Specifies the highlighted key word count * @param originString original string * @param keyWords highlighted string array * @param highLightColor highLightColor value * @return highlighted string */ public static SpannableString getHighLightWordsArray(String originString, String[] keyWords, int highLightColor) { SpannableString originSpannableString = new SpannableString(originString); if (keyWords ! = null && keyWords.length > 0) { for (int i = 0; i < keyWords.length; i++) { Pattern p = Pattern.compile(keyWords[i]); Matcher m = p.matcher(originSpannableString); while (m.find()) { int start = m.start(); int end = m.end(); originSpannableString.setSpan(new ForegroundColorSpan(highLightColor), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } } return originSpannableString; }Copy the code

conclusion

So no matter what needs, can not be satisfied, just product manager to mention, what to give what