Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created October 28, 2015 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaspili/4878ef49c607efe1e7d0 to your computer and use it in GitHub Desktop.
Save lukaspili/4878ef49c607efe1e7d0 to your computer and use it in GitHub Desktop.
package com.siu.android.univelo.paris.ui.view.util
import android.content.res.AssetManager
import android.graphics.Typeface
import android.view.View
import java.util.*
/**
* @author Lukasz Piliszczuk - lukasz.pili@gmail.com
*/
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