如何使用 SpannableString 更改行 space?
How to change line space with a SpannableString?
对于一个项目,我必须使用粗体、斜体、大小等属性在 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 中行与行之间的行 space。为此,我想改变底部或填充,但我不知道该怎么做。你有什么想法吗?
感谢您的帮助,祝您愉快!
也许你可以通过 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
}
对于一个项目,我必须使用粗体、斜体、大小等属性在 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 中行与行之间的行 space。为此,我想改变底部或填充,但我不知道该怎么做。你有什么想法吗?
感谢您的帮助,祝您愉快!
也许你可以通过 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
}