Skip to content

Instantly share code, notes, and snippets.

@alorma
Created February 6, 2015 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alorma/e4f565142559071d0b00 to your computer and use it in GitHub Desktop.
Save alorma/e4f565142559071d0b00 to your computer and use it in GitHub Desktop.
public class FontUtils {
public static void wrap(TextView textView) {
if (textView != null) {
try {
String fontName = FontUtils.fontName(textView);
if (fontName != null) {
Typeface typeface = Typeface.createFromAsset(textView.getContext().getAssets(), "fonts/" + fontName);
textView.setTypeface(typeface);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static String fontName(TextView textView) {
if (textView.getTypeface() != null) {
int style = textView.getTypeface().getStyle();
if (textView.getTypeface().isBold()) {
return "bold.otf";
} else if (textView.getTypeface().isItalic()) {
return "italic.otf";
} else if ((style & Typeface.BOLD_ITALIC) != 0) {
return "bold_italic.otf";
}
}
return "regular.otf";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment