Android 引入字體包

繼承AppCompatTextView引入字體包,后續(xù)直接使用MyFontTextView 即可

open class MyFontTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr) {

    private var currentWeight = 400
    private var isItalic = false

    init {
        applyCustomFont(context, attrs)
    }

    private fun applyCustomFont(context: Context, attrs: AttributeSet?) {

        if (attrs != null) {
            for (i in 0 until attrs.attributeCount) {
                when (attrs.getAttributeNameResource(i)) {
                    android.R.attr.textFontWeight -> {
                        currentWeight = attrs.getAttributeIntValue(i, 400)
                    }
                    android.R.attr.textStyle -> {
                        val style = attrs.getAttributeIntValue(i, Typeface.NORMAL)
                        isItalic = (style == Typeface.ITALIC || style == Typeface.BOLD_ITALIC)
                    }
                }
            }

        }
        typeface = FontCache.getInterTypeface(context, currentWeight, isItalic)
    }

    fun setFontWeight(weight: Int) {
        if (currentWeight != weight) {
            currentWeight = weight
            updateCustomTypeface()
        }
    }

    private fun updateCustomTypeface() {
        val newTypeface = FontCache.getInterTypeface(context, currentWeight, isItalic)
        if (typeface != newTypeface) {
            typeface = newTypeface
        }
    }
}

字體包緩存類


import android.content.Context
import android.graphics.Typeface
import androidx.core.content.res.ResourcesCompat

object FontCache {
    private val cache = mutableMapOf<String, Typeface?>()
    private val weights = listOf(400, 500, 600, 700, 800, 900)
    private val italics = listOf(false, true)

    fun init(context: Context) {
        for (weight in weights) {
            for (italic in italics) {
                getInterTypeface(context, weight, italic)
            }
        }
    }

    /**
     * 獲取 Inter 字體,根據(jù)權(quán)重 & italic 自動(dòng)選擇
     */
    fun getInterTypeface(context: Context, weight: Int, italic: Boolean): Typeface? {
        val key = "$weight-$italic"
        cache[key]?.let { return it }
        val resId = when (weight) {
            500 -> R.font.inter_medium_500
            600 -> R.font.inter_semi_bold_600
            700 -> R.font.inter_bold_700
            800 -> R.font.inter_extra_bold_800
            900 -> R.font.inter_display_black_900
            else -> R.font.inter_regular_400
        }
        var typeface = ResourcesCompat.getFont(context, resId)
        if (italic && typeface != null) {
            typeface = Typeface.create(typeface, Typeface.ITALIC)
        }
        cache[key] = typeface
        return typeface
    }
}

使用

(可選項(xiàng))可以在Application里面對(duì)字體包進(jìn)行提前加載,調(diào)用FontCache.init(this)即可

<com.xxx.xxx.widget.MyFontTextView
    android:textFontWeight="600"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:includeFontPadding="false"
    android:textColor="@color/white"
    android:textSize="14sp"
    tools:text="Level 1 Club" />

擴(kuò)展函數(shù)

fun TextView.setInterWeight(weight: Int, italic: Boolean = false) {
    val newTypeface = FontCache.getInterTypeface(context, weight, italic)
    if (this.typeface != newTypeface) {
        this.typeface = newTypeface
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容