Android 测量文本宽度方式

Android 测量文本宽度方式

val str = "hello world"
val paint = TextPaint()
paintEnd.textSize = xxx
paintEnd.typeface = xxx

1.measureText

🍎 该方式会受到 文字大小(textSize)字体(typeface) 的影响

paint.measureText(str)

2. getTextBounds

val rect = Rect()
paint.getTextBounds(str, 0, str.length, rect)
val w: Int = rect.width()

3. getDesiredWidth

android.text.Layout.getDesiredWidth(str, paint)

解决 ClickableSpan 和 ForegroundColorSpan 颜色冲突问题

val clickableSpan: ClickableSpan = object : ClickableSpan() {
    override fun onClick(widget: View?) {
    }
    
    override fun updateDrawState(ds: TextPaint?) {
        //super.updateDrawState(ds)
        //ds?.color = ds?.linkColor //解决 ClickableSpan 和 ForegroundColorSpan 颜色冲突问题
        ds?.isUnderlineText = false //去掉下划线
    }
}

本文作者:风之旅人

本文链接:https://www.cnblogs.com/jooy/p/17302017.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   javakam  阅读(0)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起