LineHeightFactor and LineSpace with Spannable

Viewed 117

I need to use an EditText with Span to customize the text. For my app, I need two parameters for my spans :

  • Line space
  • Line Height factor

I've tried to use the interface LineHeightSpanfor the first param like that :

class LineSpaceSpan(private val space : Int) : LineHeightSpan {

        override fun chooseHeight(
                text: CharSequence?,
                start: Int,
                end: Int,
                spanstartv: Int,
                lineHeight: Int,
                fm: Paint.FontMetricsInt?) {

                fm?.apply {
                    descent += space
                    bottom += space
                }
        }   
}

But the result isn't really what I expect...

For Line Height factor I know there an XML attribute in EditText, but I need a different value for each line

Has anyone ever had these needs? Thank you for your help!

EDIT: More infos

I've tried to solve my problem with LineHeightSpan like above with that code :

class LineSpaceSpan(private var lineRatio: Float, private var space : Int) : LineHeightSpan {

        override fun chooseHeight(
                text: CharSequence?,
                start: Int,
                end: Int,
                spanstartv: Int,
                lineHeight: Int,
                fm: Paint.FontMetricsInt?) {

                fm?.apply {

                    val currentHeight = it.descent - it.ascent
                    val targetHeight = lineRatio * currentHeight

                    val heightMargin = (targetHeight - currentHeight) / 2

                    // Set line height
                    it.descent += heightMargin.roundToInt()
                    it.ascent -= heightMargin.roundToInt()

                    // Set line space
                    it.descent += space
                    it.bottom += space
                }
        }   
}

but I've some troubles if I change values for lineRatio or space :

  • LineHeight doesn't change
  • LineSpace, instead of change space with next span, it goes up or down the text inside the current span

EDIT : SOLUTION

I've found the problem... First, the code above is working ! The problem came from my selection, I did not select the line entirely (the "\n" at the end)

0 Answers
Related