英文:
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具有较大的填充?
英文:
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
)
),
)
)
}
}
}
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论