上一篇文章说到 因为一个集成了AutofitTextView导致了ANR,所以只能全局替换掉继承关系,还得支持字体自动缩放,然后就有了这玩意
open class AutoFontTextView : AppCompatTextView {
private var mMaxTextSize: Int = 0
private var mMinTextSize: Int = 0
private var mWidthMeasureSpec: Int? = null
private var mHeightMeasureSpec: Int? = null
private var mIsSizeToFit = false
private var mIsInList = false
companion object {
private val typefaceCache = mutableMapOf<String, Typeface>()
val defaultTypefaceMap = mapOf<DefaultTypeFace, String>(
Pair(DefaultTypeFace.BarlowMedium, "Barlow-Medium.ttf"),
Pair(DefaultTypeFace.BarlowRegular, "Barlow-Regular.ttf"),
Pair(DefaultTypeFace.BarlowSemiBold, "Barlow-SemiBold.ttf"),
Pair(DefaultTypeFace.SourceHanSansCNMedium, "SourceHanSansCN-Medium.otf"),
Pair(DefaultTypeFace.SourceHanSansCNRegular, "SourceHanSansCN-Regular.otf")
)
fun createTypeface(context: Context, path: String): Typeface {
return typefaceCache[path] ?: Typeface.createFromAsset(context.assets, path).apply {
typefaceCache[path] = this
}
}
}
enum class DefaultTypeFace {
BarlowMedium, BarlowRegular, BarlowSemiBold, SourceHanSansCNMedium, SourceHanSans