So i’m trying to replicate the maxHeightInLinesMod...
# compose
m
So i’m trying to replicate the maxHeightInLinesModifier from compose 1.4 so that it can work with existing versions. I’m having some trouble with the embedded calculations. For whatever reason, it things the paragraph with a single line is 59px tall, and that the paragraph with 2 lines is 111px tall. I would have expected the 2 line paragraph to be at least twice as tall (118px) plus some additional for the line height on the text style. Not sure what I might be missing here.
Copy code
@Composable
@Preview
fun TestParagraph() {
    val density = LocalDensity.current
    val fontFamilyResolver = LocalFontFamilyResolver.current
    val placeholderText = "H".repeat(10)

    val paragraph1 = Paragraph(
        text = placeholderText,
        style = MaterialTheme.typography.body1,
        spanStyles = listOf(),
        maxLines = 1,
        ellipsis = false,
        density = density,
        fontFamilyResolver = fontFamilyResolver,
        constraints = Constraints()
    )

    val paragraph2 = Paragraph(
        text = (1.. 2).map { placeholderText }.joinToString("\n"),
        style = MaterialTheme.typography.body1,
        spanStyles = listOf(),
        maxLines = 2,
        ellipsis = false,
        density = density,
        fontFamilyResolver = fontFamilyResolver,
        constraints = Constraints()
    )

    val height1 = paragraph1.height
    val height2 = paragraph2.height

    Column {
        Text(text = "${height1}")
        Text(text = "${height2}")
    }
}
message has been deleted
for reference it’s at least consistent in that it increases by 52px for every additional line i add.
k
Where’s your backported modifier code?
m
@Kirill Grouchnikov i’m not worried about the modifier code yet to be honest. It’s not 100% necessary. I’m just curious about the heights and why the first line seems to occupy more average space per line than when you have multiple lines.
I’ve generally solved my issues, and this algortthm does work. The problem was on my end in that i was using a modifier on BasicTextField instead of one on the decorator box. As such, i was only seeing like 4 lines when i expected 6. Fixing that solves my issues.
k
There's a lot that goes into laying out text and computing its bounds. Things don't just scale linearly in every single case.
m
I get that, i wasn’t expecting linear, just the numbers surprised me for sure.
Soon enough BasicTextField will have the “minLines” parameter anyway. This is just a stopgap until it does and we can get onto compose 1.4.0 after it’s released. We have to get to compose 1.3 first (which requires api level 33, which we’re workign on )
a
This article, especially this image, explains how line height calculation works and why the case you described is possible.
m
Thanks @Albert Chang. That explains it. The additional top and bottom padding is what is adding to the measurement on the single line version of the string. So it's generally going to be linear + some top and bottom padding.