-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathEnrichedLineHeightSpan.kt
More file actions
40 lines (35 loc) · 1.17 KB
/
Copy pathEnrichedLineHeightSpan.kt
File metadata and controls
40 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.swmansion.enriched.textinput.spans
import android.graphics.Paint
import android.text.Spannable
import android.text.TextPaint
import android.text.style.LineHeightSpan
import android.text.style.MetricAffectingSpan
import com.swmansion.enriched.common.pixelFromSpOrDp
import com.swmansion.enriched.common.spans.interfaces.EnrichedHeadingSpan
class EnrichedLineHeightSpan(
val lineHeight: Float,
val allowFontScaling: Boolean,
) : MetricAffectingSpan(),
LineHeightSpan {
override fun updateDrawState(p0: TextPaint?) {
// Do nothing but inform TextView that line height should be recalculated
}
override fun updateMeasureState(p0: TextPaint) {
// Do nothing but inform TextView that line height should be recalculated
}
override fun chooseHeight(
text: CharSequence,
start: Int,
end: Int,
spanstartv: Int,
v: Int,
fm: Paint.FontMetricsInt,
) {
val lineHeightPx = pixelFromSpOrDp(lineHeight, allowFontScaling)
val currentHeight = (fm.descent - fm.ascent).toFloat()
if (lineHeightPx <= currentHeight) return
val extra = (lineHeightPx - currentHeight).toInt()
fm.ascent -= extra
fm.top = minOf(fm.top, fm.ascent)
}
}