Created
December 13, 2015 15:42
-
-
Save theshapguy/7c5d562ec8f8db4a45cb to your computer and use it in GitHub Desktop.
Font Cache in Kotlin
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
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