Created
June 5, 2018 06:27
-
-
Save pfieffer/6f80b9aa7cef94c0383dfb17d2646f77 to your computer and use it in GitHub Desktop.
A utility class to load custom font from the assets/font folder on Views
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 CustomFontLoader { | |
public static final int FONT_NAME_1 = 0; | |
public static final int FONT_NAME_2 = 1; | |
private static final int NUM_OF_CUSTOM_FONTS = 2; | |
private static boolean fontsLoaded = false; | |
private static Typeface[] fonts = new Typeface[2]; | |
private static String[] fontPath = { | |
"font/AvenirNextLTPro-Demi.otf", | |
"font/AvenirNextLTPro-Regular.otf", | |
}; | |
/** | |
* Returns a loaded custom font based on it's identifier. | |
* | |
* @param context - the current context | |
* @param fontIdentifier = the identifier of the requested font | |
* @return Typeface object of the requested font. | |
*/ | |
public static Typeface getTypeface(Context context, int fontIdentifier) { | |
if (!fontsLoaded) { | |
loadFonts(context); | |
} | |
return fonts[fontIdentifier]; | |
} | |
private static void loadFonts(Context context) { | |
for (int i = 0; i < NUM_OF_CUSTOM_FONTS; i++) { | |
fonts[i] = Typeface.createFromAsset(context.getAssets(), fontPath[i]); | |
} | |
fontsLoaded = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment