当前位置:网站首页>Kotlin Compose Text支持两种颜色

Kotlin Compose Text支持两种颜色

2022-07-06 21:38:00 安果移不动

首先可以用原生去渲染html

@Composable
fun TitleByYellow(
    textAll: String,
    textYellow: String? = null,
    textSizeSp: Float = 15F,
    @ColorRes textColorRes: Int = R.color.color_4A4A4A,
    isBold: Boolean = false
) {
    var text = textAll
    if (!TextUtils.isEmpty(textYellow)) {
        text = textAll.format("<font color='#F6CA2D'>$textYellow</font>")
    }

    val htmlDescription = Html.fromHtml(text)

    AndroidView(factory = { context ->
        val tv = TextView(context)
        tv.apply {
            textSize = textSizeSp
            setTextColor(context.getColor(textColorRes))
            if (isBold) {
                setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            }
        }
        tv
    },
        update = {
            it.text = htmlDescription
        }
    )
}

原网站

版权声明
本文为[安果移不动]所创,转载请带上原文链接,感谢
https://blog.csdn.net/mp624183768/article/details/125632990