gpt4 book ai didi

java - 如何使用 SpannableString 更改行距?

转载 作者:行者123 更新时间:2023-12-02 12:46:14 27 4
gpt4 key购买 nike

对于一个项目,我必须在 EditText 中使用一些属性编辑文本,例如粗体、斜体、大小等。因此,我制作了一个 CustomSpan,它实现了 StyleSpan,具有我需要的所有属性。在我的代码下面:

class CustomSpan(
val bold: Boolean = false,
val italic: Boolean = false,
val size: Int = 14,
val color: Int = Color.BLACK,
val letterSpacing: Float = 0f
): StyleSpan(when {
bold && italic -> Typeface.BOLD_ITALIC
bold -> Typeface.BOLD
italic -> Typeface.ITALIC
else -> Typeface.NORMAL
}) {

override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)

ds.color = color
ds.textSize = size.toFloat()
ds.letterSpacing = letterSpacing
}

override fun updateMeasureState(paint: TextPaint) {
super.updateMeasureState(paint)

paint.color = color
paint.textSize = size.toFloat()
paint.letterSpacing = letterSpacing
}

fun copy() : CustomSpan = CustomSpan(bold, italic, size, color, font, letterSpacing)

fun copyWith(bold: Boolean? = null,
italic: Boolean? = null,
size: Int? = null,
color: Int? = null,
letterSpacing: Float? = null) : CustomSpan {

return CustomSpan(
bold = bold ?: this.bold,
italic = italic ?: this.italic,
size = size ?: this.size,
color = color ?: this.color,
letterSpacing = letterSpacing ?: this.letterSpacing
)
}
}

但是,我还需要更改 EditText 中行之间的行距。为此,我想改变底部或填充,但我不知道该怎么做。你有什么想法吗?

感谢您的帮助,祝您有美好的一天!

最佳答案

也许你可以通过 baselineShift 实现这一点。 ,你可以尝试这样的事情:

override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)

ds.color = color
ds.textSize = size.toFloat()
ds.letterSpacing = letterSpacing

// here you might want to play with a specific value or ds.ascent/ds.descent..
ds.baselineShift += 12
}

关于java - 如何使用 SpannableString 更改行距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60213035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com