Problem with lineHeight in Text Jetpack Compose

huangapple go评论61阅读模式
英文:

Problem with lineHeight in Text Jetpack Compose

问题

I have some problem with lineHeight in Text widget. Let me show what I mean on an example. I have two columns with two types of Text widgets. The first has a lineHeight equal to 2.em, and the second has 0.em.

Row(Modifier.fillMaxWidth()) {
    Column {
        repeat(10) {
            Text(
                text = "Test1-$it",
                style = TextStyle(
                    fontSize = 20.sp,
                    fontWeight = FontWeight(500),
                    lineHeight = 2.em,
                    lineHeightStyle = LineHeightStyle(
                        alignment = LineHeightStyle.Alignment.Center,
                        trim = LineHeightStyle.Trim.None
                    )
                ),
            )
        }
    }
    Column {
        repeat(10) {
            Text(
                text = "Test2-$it",
                style = LocalTextStyle.current.merge(
                    TextStyle(
                        fontSize = 20.sp,
                        fontWeight = FontWeight(500),
                        lineHeight = 0.em,
                        lineHeightStyle = LineHeightStyle(
                            alignment = LineHeightStyle.Alignment.Center,
                            trim = LineHeightStyle.Trim.None
                        )
                    ),
                )
            )
        }
    }
}

为什么具有较小lineHeight的Text具有较大的填充?

Problem with lineHeight in Text Jetpack Compose

英文:

I have some problem with lineHeight in Text wiget.
Let me show what i mean on example.
I have two columns with two types Text widgets. First have lineHeight eaqual 2.em, second 0.em.

Row(Modifier.fillMaxWidth()) {
    Column {
        repeat(10) {
            Text(
                text = "Test1-$it",
                style = TextStyle(
                    fontSize = 20.sp,
                    fontWeight = FontWeight(500),
                    lineHeight = 2.em,
                    lineHeightStyle = LineHeightStyle(
                        alignment = LineHeightStyle.Alignment.Center,
                        trim = LineHeightStyle.Trim.None
                    )
                ),
            )
        }
    }
    Column {
        repeat(10) {
            Text(
                text = "Test2-$it",
                style = LocalTextStyle.current.merge(
                    TextStyle(
                        fontSize = 20.sp,
                        fontWeight = FontWeight(500),
                        lineHeight = 0.em,
                        lineHeightStyle = LineHeightStyle(
                            alignment = LineHeightStyle.Alignment.Center,
                            trim = LineHeightStyle.Trim.None
                        )
                    ),
                )
            )
        }
    }
}

Why Text with smaller lineHeight have bigger paddings?
Problem with lineHeight in Text Jetpack Compose

答案1

得分: 1

实际上,这种情况发生的是,当将 0.em 作为行高提供时,它会导致 Compose 使用默认的行高,这个行高大于 2.em。尝试在 Test2 中使用 1.em 而不是 0.em,然后查看结果。

英文:

Actually, what's happening in this scenario is that when providing 0.em as line height, it'll cause Compose to use the default line size height which is bigger than 2.em. Try using 1.em for Test2 instead of 0.em and see the results.

huangapple
  • 本文由 发表于 2023年5月30日 00:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定