Skip to content

Instantly share code, notes, and snippets.

@bholota
Created January 12, 2016 14:39
Show Gist options
  • Select an option

  • Save bholota/4e230bb4a2ed4b810edc to your computer and use it in GitHub Desktop.

Select an option

Save bholota/4e230bb4a2ed4b810edc to your computer and use it in GitHub Desktop.
Sets regular font face for regex matches and light one for rest
private Spannable setLightHeaderText(String text, String regexPattern) {
final Pattern regPattern = Pattern.compile(regexPattern);
final Matcher matches = regPattern.matcher(text);
final Spannable spannable = new SpannableString(text);
// right now we use first only occurrence if exist
if (!matches.find()) {
final CustomTypeFaceSpan lightSpanStart = new CustomTypeFaceSpan("roboto-light");
spannable.setSpan(lightSpanStart, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}
spannable.setSpan(new CustomTypeFaceSpan("roboto-light"), 0, text.length(), Spanned.SPAN_COMPOSING);
do {
int start = matches.start();
int end = matches.end();
final CustomTypeFaceSpan regularSpan= new CustomTypeFaceSpan("roboto-regular");
spannable.setSpan(regularSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} while (matches.find());
return spannable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment