The answer above doesn't work for me, but the basic idea is correct, adding an empty line and changing its height gives the desired result. However, a working way of changing the line height in my case is as follows:
private fun insertParagraphSpacing(text: CharSequence, spacing: Int): CharSequence {
val stringBuilder = SpannableStringBuilder(text)
Regex(lineSeparator()).findAll(text).forEachIndexed { index, match ->
val span = ParagraphSpacingSpan(lineHeight, spacing)
val separator = SpannableString(lineSeparator())
separator.setSpan(span, 0, separator.length, SPAN_EXCLUSIVE_EXCLUSIVE)
stringBuilder.insert(match.range.last + 1 + index * separator.length, separator)
}
return stringBuilder
}
And the implementation of ParagraphSpacingSpan
is as follows:
class ParagraphSpacingSpan(
@Px @IntRange(from = 1) val lineHeight: Int,
@Px @IntRange(from = 0) val spacing: Int
) : LineHeightSpan {
override fun chooseHeight(t: CharSequence, s: Int, e: Int, v: Int, l: Int, fm: FontMetricsInt) {
val textHeight: Int = fm.descent - fm.ascent
fm.descent = textHeight - lineHeight + spacing
fm.ascent = 0
}
}
lineHeight
is the value of the TextView
field in which the line is displayed.