Created
April 30, 2021 03:45
-
-
Save kakajika/9d5d065af3216fba09e4497b1b20f44e to your computer and use it in GitHub Desktop.
LineHeightSpan.Standard for lower API versions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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