如何使用spannablestring更改行空间?

xt0899hw  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(296)

对于一个项目,我必须在一个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中行之间的行间距。为此,我想改变底部或填充,但我不知道怎么做。你有什么想法吗?
谢谢你的帮助,祝你有愉快的一天!

eaf3rand

eaf3rand1#

也许你可以通过 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
}

相关问题