Skip to content

Instantly share code, notes, and snippets.

@voghDev
Last active November 3, 2016 08:52
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 voghDev/6b83aa0fd72fafc361aae1928d6abc98 to your computer and use it in GitHub Desktop.
Save voghDev/6b83aa0fd72fafc361aae1928d6abc98 to your computer and use it in GitHub Desktop.
TextView subclass that applies a customized font. You also need FontFactory class, available in the first comment of this gist
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class StyledTextView extends TextView {
private static final int NO_STYLE = -1;
public StyledTextView(Context context) {
super(context);
style(null, NO_STYLE);
}
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(attrs, NO_STYLE);
}
public StyledTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
style(attrs, defStyleAttr);
}
private void style(AttributeSet attrs, int defStyle) {
if (isInEditMode())
return;
FontFactory factory = new FontFactory(getContext());
int style = getTypeface() != null ? getTypeface().getStyle() : Typeface.NORMAL;
Typeface tf = factory.getRegularTypeface();
if (attrs != null) {
}
if (style == Typeface.BOLD)
tf = factory.getBoldTypeface();
else if (style == Typeface.ITALIC)
tf = factory.getItalicTypeface();
setTypeface(tf);
}
}
@voghDev
Copy link
Author

voghDev commented Nov 3, 2016

import android.content.Context;
import android.graphics.Typeface;

public class FontFactory {
    protected static Typeface sTypefaceBold = null;
    protected static Typeface sTypefaceRegular = null;
    protected static Typeface sTypefaceLight = null;

    protected static final String FONTS_FOLDER = "fonts/";
    public static final String DEFAULT_BOLD_TYPEFACE_NAME = FONTS_FOLDER + "MyFont-Bold.ttf";
    public static final String DEFAULT_REGULAR_TYPEFACE_NAME = FONTS_FOLDER + "MyFont-Regular.ttf";
    public static final String DEFAULT_ITALIC_TYPEFACE_NAME = FONTS_FOLDER + "MyFont-Italic.ttf";
    private static FontFactory instance;

    Context context;

    public FontFactory(Context context) {
        this.context = context;
    }

    public Typeface getBoldTypeface() {
        if (sTypefaceBold == null) {
            sTypefaceBold = getTypeface(DEFAULT_BOLD_TYPEFACE_NAME, Typeface.BOLD);
        }
        return sTypefaceBold;
    }

    public Typeface getItalicTypeface() {
        if (sTypefaceLight == null) {
            sTypefaceLight = getTypeface(DEFAULT_ITALIC_TYPEFACE_NAME, Typeface.NORMAL);
        }
        return sTypefaceLight;
    }

    public Typeface getRegularTypeface() {
        if (sTypefaceRegular == null) {
            sTypefaceRegular = getTypeface(DEFAULT_REGULAR_TYPEFACE_NAME, Typeface.NORMAL);
        }
        return sTypefaceRegular;
    }

    public Typeface getTypeface(String resourceName, int defaultStyle) {
        return Typeface.createFromAsset(context.getAssets(), resourceName);
    }

    public Typeface getTypeface(int style) {
        if (style == Typeface.BOLD) {
            return getBoldTypeface();
        } else {
            return getRegularTypeface();
        }
    }

    public static FontFactory getInstance(Context context) {
        if (instance == null) {
            instance = new FontFactory(context);
        }
        return instance;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment