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 //去掉下划线
    }
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享