Skip to content

Instantly share code, notes, and snippets.

@arthtilva
Created August 31, 2016 06:20
Show Gist options
  • Save arthtilva/82b835cb1a1abf4d6473271383c4da92 to your computer and use it in GitHub Desktop.
Save arthtilva/82b835cb1a1abf4d6473271383c4da92 to your computer and use it in GitHub Desktop.
Custom Font TextView
public class CTextView extends TextView {
public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public CTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context, attrs);
}
public CTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context, attrs);
}
private void applyCustomFont(Context context, AttributeSet attrs) {
int textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
Typeface customFont = selectTypeface(context, textStyle);
setTypeface(customFont);
}
private Typeface selectTypeface(Context context, int textStyle) {
switch (textStyle) {
case Typeface.BOLD:
return Typeface.createFromAsset(context.getAssets(), "DIN Next W01 Regular.ttf");
case Typeface.NORMAL:
return Typeface.createFromAsset(context.getAssets(), "DIN Next W01 Light.ttf");// regular
default:
return Typeface.createFromAsset(context.getAssets(), "DIN Next W01 Light.ttf");// regular
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment