Created
January 5, 2018 08:07
-
-
Save nguyenlinhnttu/4fba4e555a422e347a08eedab0a22b92 to your computer and use it in GitHub Desktop.
Custom TextView/Button/ ------- Cache Font
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
public class CustomFontTextView extends AppCompatTextView { | |
private static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android"; | |
public CustomFontTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
applyCustomFont(context, attrs); | |
} | |
public CustomFontTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
applyCustomFont(context, attrs); | |
} | |
private Typeface selectTypeface(Context context, int style) { | |
switch (style) { | |
case Typeface.BOLD: | |
return FontCache.getTypeface(context, Font.FIRASANS_BOLD); | |
case Typeface.ITALIC: | |
return FontCache.getTypeface(context, Font.FIRASANS_ITALIC); | |
case Typeface.BOLD_ITALIC: | |
return FontCache.getTypeface(context, Font.FIRASANS_BOLD_ITALIC); | |
case Typeface.NORMAL: | |
default: | |
return FontCache.getTypeface(context, Font.FIRASANS); | |
} | |
} | |
private void applyCustomFont(Context context, AttributeSet attrs) { | |
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL); | |
Typeface customFont = selectTypeface(context, textStyle); | |
setTypeface(customFont); | |
} | |
} |
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
@Retention(RetentionPolicy.SOURCE) | |
@StringDef({ | |
FIRASANS, | |
FIRASANS_BOLD, | |
FIRASANS_BOLD_ITALIC, | |
FIRASANS_ITALIC | |
}) | |
public @interface Font { | |
String FIRASANS = "MuseoSansRounded-300.otf"; | |
String FIRASANS_BOLD = "MuseoSansRounded-700.otf"; | |
String FIRASANS_ITALIC = "FiraSans-Italic.ttf"; | |
String FIRASANS_BOLD_ITALIC = "FiraSans-BoldItalic.ttf"; | |
} |
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
public class FontCache { | |
private static HashMap<String, Typeface> fontCache = new HashMap<>(); | |
public static Typeface getTypeface(Context context, @Font String fontName) { | |
Typeface typeface = fontCache.get(fontName); | |
if (typeface == null) { | |
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontName); | |
fontCache.put(fontName, typeface); | |
} | |
return typeface; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment