Created
December 15, 2016 15:50
-
-
Save dGorod/c26831c4c5aa64d3acef7c7776ef4197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.askmirrormirror.android.presentation.common; | |
import android.support.annotation.ColorInt; | |
import android.text.Editable; | |
import android.text.Spanned; | |
import android.text.TextWatcher; | |
import android.text.style.CharacterStyle; | |
import android.text.style.ForegroundColorSpan; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* Simple TextWatcher to highlight all tags. | |
* Default trigger is '#'. But you can provide your own set. | |
* | |
* Created by: | |
* @author Dmytro Gorodnytskyi | |
* on 12-Dec-16. | |
*/ | |
public class TagWatcher implements TextWatcher { | |
private static char mTrigger = '#'; | |
private boolean mCheckText; | |
private int mColor; | |
private List<CharacterStyle> mSpans; | |
private Pattern mPattern; | |
private char[] mExternalTriggers; | |
public TagWatcher(@ColorInt int highlightColor) { | |
mSpans = new ArrayList<>(); | |
mColor = highlightColor; | |
mPattern = Pattern.compile("#\\w+"); | |
} | |
public TagWatcher(@ColorInt int highlightColor, char[] triggers) { | |
mSpans = new ArrayList<>(); | |
mColor = highlightColor; | |
mPattern = Pattern.compile(buildPattern(triggers)); | |
mExternalTriggers = triggers; | |
} | |
@Override | |
public void beforeTextChanged(CharSequence s, int start, int count, int after) { } | |
@Override | |
public void onTextChanged(CharSequence s, int start, int before, int count) { | |
for (int i = 0; i < s.length(); i++) { | |
if (mExternalTriggers == null && s.charAt(i) == mTrigger) { | |
mCheckText = true; | |
return; | |
} | |
else if (mExternalTriggers != null) { | |
for (char trigger : mExternalTriggers) { | |
if (s.charAt(i) == trigger) { | |
mCheckText = true; | |
return; | |
} | |
} | |
} | |
} | |
} | |
@Override | |
public void afterTextChanged(Editable s) { | |
if (mCheckText) { | |
mCheckText = false; | |
clearSpans(s); | |
addSpans(s); | |
} | |
} | |
private String buildPattern(char[] triggers) { | |
StringBuilder str = new StringBuilder("("); | |
for (int i = 0; i < triggers.length; i++) { | |
str.append(triggers[i]); | |
if (i < triggers.length - 1) { | |
str.append('|'); | |
} | |
} | |
str.append(")\\w+"); | |
return str.toString(); | |
} | |
private void clearSpans(Editable s) { | |
for (CharacterStyle span : mSpans) { | |
s.removeSpan(span); | |
} | |
mSpans.clear(); | |
} | |
private void addSpans(Editable s) { | |
Matcher matcher = mPattern.matcher(s); | |
while (matcher.find()) { | |
ForegroundColorSpan span = new ForegroundColorSpan(mColor); | |
s.setSpan(span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | |
mSpans.add(span); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment