Skip to content

Instantly share code, notes, and snippets.

@lesleh
Last active December 29, 2015 01:29
Show Gist options
  • Save lesleh/7593855 to your computer and use it in GitHub Desktop.
Save lesleh/7593855 to your computer and use it in GitHub Desktop.
Custom font handling for Android, which caches the typefaces. Place the fonts in /assets/fonts/.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomFont">
<attr name="font" format="string"/>
</declare-styleable>
</resources>
import android.content.res.AssetManager;
import android.graphics.Typeface;
import java.util.HashMap;
import java.util.Map;
public class CustomFontManager {
private static Map<String, Typeface> typefaces = new HashMap<String, Typeface>();
private CustomFontManager() {
// No instances
}
public static Typeface getTypeface(AssetManager assetManager, String name) {
if(typefaces.containsKey(name))
return typefaces.get(name);
else
return loadTypeface(assetManager, name);
}
private static synchronized Typeface loadTypeface(AssetManager assetManager, String name) {
if(typefaces.containsKey(name))
return typefaces.get(name);
Typeface typeface = null;
try {
typeface = Typeface.createFromAsset(assetManager, name);
} catch(RuntimeException e) {
throw new IllegalArgumentException("Unable to load typeface. Did you remember to add it to assets/fonts?", e);
}
typefaces.put(name, typeface);
return typeface;
}
}
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomFontTextView extends TextView {
public CustomFontTextView(Context context) {
super(context);
init(context, null, 0);
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
CustomFontUtils.parseAttributes(this, context, attrs, defStyle);
}
}
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomFontUtils {
private CustomFontUtils() {
// No instances
}
public static void parseAttributes(TextView textView, Context context, AttributeSet attrs, int defStyle) {
if(textView.isInEditMode())
return; // Typeface loading isn't implemented in the IDE
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
try {
String font = a.getString(R.styleable.CustomFont_font);
if(font != null)
textView.setTypeface(CustomFontManager.getTypeface(context.getAssets(), "fonts/" + font));
} finally {
a.recycle();
}
}
}
<com.example.CustomFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
app:font="RobotoSlab-Regular.ttf"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment