Skip to content

Instantly share code, notes, and snippets.

Created October 28, 2015 21:16
What would you like to do?
import android.content.res.AssetManager
import android.view.View
import java.util.*
* @author Lukasz Piliszczuk -
private val fonts = HashMap<String, Typeface>()
private const val FONT_AVENIR_LIGHT = "Avenir-Light.ttf"
private const val FONT_HELVETICA = "Helvetica.ttf"
private const val FONT_HELVETICA_LIGHT = "Helvetica-Light.ttf"
private const val FONT_HEITI_LIGHT = "STHeiti-TC-Light.ttf"
private const val FONT_HEITI_MEDIUM = "STHeiti-TC-Medium.ttf"
private fun load(assetManager: AssetManager, filePath: String): Typeface? {
if (!fonts.containsKey(filePath)) {
val typeface = Typeface.createFromAsset(assetManager, filePath)
fonts.put(filePath, typeface)
return typeface
return fonts[filePath]
fun View.font(name: String) = load(context.assets, "fonts/$name")
fun View.avenirLight() = font(FONT_AVENIR_LIGHT)
fun View.helvetica() = font(FONT_HELVETICA)
fun View.helveticaLight() = font(FONT_HELVETICA_LIGHT)
fun View.heitiLight() = font(FONT_HEITI_LIGHT)
fun View.heitiMedium() = font(FONT_HEITI_MEDIUM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment