Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LineHeightSpan.Standard for lower API versions
import android.os.Build
import android.text.style.LineHeightSpan
import androidx.annotation.Px
import kotlin.math.roundToInt
object LineHeightSpanCompat {
fun standard(@Px lineHeight: Int): LineHeightSpan =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
LineHeightSpan.Standard(lineHeight)
} else {
LineHeightSpan { _, _, _, _, _, fm ->
val originHeight = fm.descent - fm.ascent
if (originHeight > 0) {
val ratio = lineHeight * 1.0f / originHeight
fm.descent = (fm.descent * ratio).roundToInt()
fm.ascent = fm.descent - lineHeight
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment