Last active
January 10, 2017 01:17
-
-
Save NunciosChums/27b3e834ad476eb1970857fce0d8cff9 to your computer and use it in GitHub Desktop.
This file contains 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
textName.setTypeface(TypefaceUtil.createLightFont(getApplicationContext())); |
This file contains 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 MyApp extends Application { | |
private static Context context; | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
context = getApplicationContext(); | |
TypefaceUtil.init(context); | |
} | |
public static Context getContext() { | |
return context; | |
} | |
} |
This file contains 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 TypefaceUtil { | |
public static final String BLACK_FONT_NAME = "fonts/BlackFont.otf"; | |
public static final String MEDIUM_FONT_NAME = "fonts/MediumFont.otf"; | |
public static final String REGULAR_FONT_NAME = "fonts/RegularFont.otf"; | |
public static final String LIGHT_FONT_NAME = "fonts/LightFont.otf"; | |
public static final String THIN_FONT_NAME = "fonts/ThinFont.otf"; | |
public static final String SANS_SERIF_BLACK = "sans-serif-black"; | |
public static final String SANS_SERIF_MEDIUM = "sans-serif-medium"; | |
public static final String SANS_SERIF = "sans-serif"; | |
public static final String SANS_SERIF_LIGHT = "sans-serif-light"; | |
public static final String SANS_SERIF_THIN = "sans-serif-thin"; | |
public static void init(Context context) { | |
Typeface black = createBlackFont(context); | |
Typeface medium = createMediumFont(context); | |
Typeface regular = createRegularFont(context); | |
Typeface light = createLightFont(context); | |
Typeface thin = createThinFont(context); | |
Map<String, Typeface> fonts = new HashMap<>(); | |
fonts.put(SANS_SERIF_BLACK, black); | |
fonts.put(SANS_SERIF_MEDIUM, medium); | |
fonts.put(SANS_SERIF, regular); | |
fonts.put(SANS_SERIF_LIGHT, light); | |
fonts.put(SANS_SERIF_THIN, thin); | |
overrideFonts(fonts); | |
overrideFont("DEFAULT", regular); | |
overrideFont("DEFAULT_BOLD", black); | |
} | |
public static Typeface createBlackFont(Context context) { | |
return Typeface.createFromAsset(context.getAssets(), BLACK_FONT_NAME); | |
} | |
public static Typeface createMediumFont(Context context) { | |
return Typeface.createFromAsset(context.getAssets(), MEDIUM_FONT_NAME); | |
} | |
public static Typeface createRegularFont(Context context) { | |
return Typeface.createFromAsset(context.getAssets(), REGULAR_FONT_NAME); | |
} | |
public static Typeface createLightFont(Context context) { | |
return Typeface.createFromAsset(context.getAssets(), LIGHT_FONT_NAME); | |
} | |
public static Typeface createThinFont(Context context) { | |
return Typeface.createFromAsset(context.getAssets(), THIN_FONT_NAME); | |
} | |
public static void overrideFonts(Map<String, Typeface> typefaces) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
try { | |
final Field field = Typeface.class.getDeclaredField("sSystemFontMap"); | |
field.setAccessible(true); | |
Map<?, ?> tempMap = (Map<?, ?>) field.get(null); | |
Map<String, Typeface> oldFonts = new HashMap<>(); | |
for (Map.Entry<?, ?> entry : tempMap.entrySet()) { | |
oldFonts.put((String) entry.getKey(), (Typeface) entry.getValue()); | |
} | |
oldFonts.putAll(typefaces); | |
field.set(null, oldFonts); | |
field.setAccessible(false); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public static void overrideFont(String fontName, Typeface typeface) { | |
try { | |
final Field field = Typeface.class.getDeclaredField(fontName); | |
field.setAccessible(true); | |
field.set(null, typeface); | |
field.setAccessible(false); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment