Last active
January 16, 2022 11:06
-
-
Save le0nidas/4ec9c5605ddd3b096032b47d87f9005c to your computer and use it in GitHub Desktop.
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
class FontAwareTextAppearanceSpan( | |
private val context: Context, | |
appearance: Int | |
) : TextAppearanceSpan(context, appearance) { | |
override fun updateMeasureState(ds: TextPaint) { | |
super.updateMeasureState(ds) | |
val font = getFont() ?: Typeface.DEFAULT | |
val oldStyle = ds.typeface?.style ?: 0 | |
ds.typeface = Typeface.create(font, oldStyle) | |
} | |
private fun getFont(): Typeface? { | |
val cleanFamilyName = family.removePrefix("res/font/").removeSuffix(".xml") | |
val appPackageName = context.applicationContext.packageName | |
val id = context.resources.getIdentifier(cleanFamilyName, "font", appPackageName) | |
return ResourcesCompat.getFont(context, id) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment