Skip to content

Commit 4a0ed95

Browse files
committed
Merge branch 'main' into feat/android-html-support
2 parents e246f9e + 369ad10 commit 4a0ed95

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.nitrotext
2+
3+
import android.graphics.Paint
4+
import android.text.style.LineHeightSpan
5+
import kotlin.math.ceil
6+
import kotlin.math.floor
7+
8+
/**
9+
* Matches React Native's CustomLineHeightSpan so lineHeight behaves the same as <Text>.
10+
*/
11+
class NitroLineHeightSpan(heightPx: Float) : LineHeightSpan {
12+
private val lineHeight: Int = ceil(heightPx.toDouble()).toInt()
13+
14+
override fun chooseHeight(
15+
text: CharSequence,
16+
start: Int,
17+
end: Int,
18+
spanstartv: Int,
19+
v: Int,
20+
fm: Paint.FontMetricsInt
21+
) {
22+
val leading = lineHeight - ((-fm.ascent) + fm.descent)
23+
val halfLeading = leading / 2f
24+
fm.ascent -= ceil(halfLeading.toDouble()).toInt()
25+
fm.descent += floor(halfLeading.toDouble()).toInt()
26+
if (start == 0) fm.top = fm.ascent
27+
if (end == text.length) fm.bottom = fm.descent
28+
}
29+
}

0 commit comments

Comments
 (0)