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