Skip to content

Instantly share code, notes, and snippets.

@kakajika
Created April 30, 2021 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kakajika/9d5d065af3216fba09e4497b1b20f44e to your computer and use it in GitHub Desktop.
Save kakajika/9d5d065af3216fba09e4497b1b20f44e to your computer and use it in GitHub Desktop.
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