Skip to content

Instantly share code, notes, and snippets.

@theshapguy
Created December 13, 2015 15:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theshapguy/7c5d562ec8f8db4a45cb to your computer and use it in GitHub Desktop.
Save theshapguy/7c5d562ec8f8db4a45cb to your computer and use it in GitHub Desktop.
Font Cache in Kotlin
object FontUtil {
private val sTypefaceCache = HashMap<String, Typeface>()
fun getOTF(context: Context, font: String): Typeface {
if (!sTypefaceCache.containsKey(font)) {
val tf = Typeface.createFromAsset(
context.applicationContext.assets, "fonts/%s.otf".format(font))
sTypefaceCache.put(font, tf)
}
return sTypefaceCache[font]!!
}
fun getTFF(context: Context, font: String): Typeface {
if (!sTypefaceCache.containsKey(font)) {
val tf = Typeface.createFromAsset(
context.applicationContext.assets, "fonts/%s.ttf".format(font))
sTypefaceCache.put(font, tf)
}
return sTypefaceCache[font]!!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment