How to change line space with a SpannableString?

Ady :

For a project, I have to edit text in an EditText with some attributes like bold, italic, size... etc. So, I have made a CustomSpan, which implements StyleSpan, with all the attributes I need. Below my code :

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
        )
    }
}

But, I also need to change the line space between lines in my EditText. For that I thought to change bottom or padding, but I don't know how to do that. Have you some ideas ?

Thank you for your help, have a nice day !

denis_lor :

Probably you could achieve that via baselineShift, you could try something like this:

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
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=7328&siteId=1
Recommended