Skip to content

Instantly share code, notes, and snippets.

@kingori
Created October 17, 2013 04:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingori/7019165 to your computer and use it in GitHub Desktop.
Save kingori/7019165 to your computer and use it in GitHub Desktop.
Android, parse and generate color, underline, bold style applied text
private final static String PATTERN_COLOR_REGEXP = "(<color=(.+?)>)(.+?)(</color>)";
private final static Pattern PATTERN_COLOR = Pattern.compile(PATTERN_COLOR_REGEXP);
private final static String PATTERN_BOLD_REGEXP = "(<b>)(.+?)(</b>)";
private final static Pattern PATTERN_BOLD = Pattern.compile(PATTERN_BOLD_REGEXP);
private final static String PATTERN_UNDERLINE_REGEXP = "(<u>)(.+?)(</u>)";
private final static Pattern PATTERN_UNDERLINE = Pattern.compile(PATTERN_UNDERLINE_REGEXP);
/**
* parse and generate color, underline, bold style applied text
* example: <![CDATA[hello <b><color=@user_name>%s</color></b>! <u><color=#ff00ff>welcome</color></u> ]]>
*
* @param text
* @return
*/
public static CharSequence applyStyle(Context context, String text) {
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
//apply color
Matcher colorMatcher = PATTERN_COLOR.matcher(text);
while (colorMatcher.find()) {
int colorVal = 0;
String colorString = colorMatcher.group(2);
if (colorString.startsWith("@")) {
colorVal =
context.getResources().getColor(
context.getResources().getIdentifier(colorString.substring(1), "color", context.getPackageName()));
} else {
colorVal = Color.parseColor(colorString);
}
ForegroundColorSpan fgSpan = new ForegroundColorSpan(colorVal);
ssb.setSpan(fgSpan, colorMatcher.start(3), colorMatcher.end(3), 0);
ssb.delete(colorMatcher.start(4), colorMatcher.end(4));
ssb.delete(colorMatcher.start(1), colorMatcher.end(1));
text = text.replaceFirst(PATTERN_COLOR_REGEXP, colorMatcher.group(3));
colorMatcher = PATTERN_COLOR.matcher(text);
}
//apply bold
Matcher boldMatcher = PATTERN_BOLD.matcher(text);
while (boldMatcher.find()) {
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
ssb.setSpan(boldSpan, boldMatcher.start(2), boldMatcher.end(2), 0);
ssb.delete(boldMatcher.start(3), boldMatcher.end(3));
ssb.delete(boldMatcher.start(1), boldMatcher.end(1));
text = text.replaceFirst(PATTERN_BOLD_REGEXP, boldMatcher.group(2));
boldMatcher = PATTERN_BOLD.matcher(text);
}
//apply underline
Matcher underlineMatcher = PATTERN_UNDERLINE.matcher(text);
while (underlineMatcher.find()) {
ssb.setSpan(new UnderlineSpan(), underlineMatcher.start(2), underlineMatcher.end(2), 0);
ssb.delete(underlineMatcher.start(3), underlineMatcher.end(3));
ssb.delete(underlineMatcher.start(1), underlineMatcher.end(1));
text = text.replaceFirst(PATTERN_UNDERLINE_REGEXP, underlineMatcher.group(2));
underlineMatcher = PATTERN_UNDERLINE.matcher(text);
}
return ssb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment