clark
11/17/2023, 6:55 PMModifier.wrapContentHeight(align = Alignment.CenterVertically
as well as using LineHeightStyle(LineHeightStyle.Alignment.Center, LineHeightStyle.Trim.Both)
but neither seem to get the job done. Code example in thread.
Anyone found something that works besides setting an offset?
SOLVED ✅
In order to get the text centered vertically, we had to add the following two properties to the text style:
platformStyle = PlatformTextStyle(includeFontPadding = false),
lineHeightStyle = LineHeightStyle(
LineHeightStyle.Alignment.Center,
LineHeightStyle.Trim.None,
),
Thank you again @francisco!clark
11/17/2023, 6:55 PM@Preview
@Composable
fun CenterTextVertically() {
val textStyle =
TextStyle(
fontSize = 14.sp,
lineHeight = 19.sp,
fontWeight = FontWeight.W500,
)
val boxModifier =
Modifier.size(16.dp).background(Color.Black, shape = RoundedCornerShape(percent = 50))
Column(
Modifier.background(Color.Green).padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Box(
boxModifier,
contentAlignment = Alignment.Center,
) {
Text(
"1",
style = textStyle,
color = Color.White,
modifier = Modifier.wrapContentHeight(align = Alignment.CenterVertically)
)
}
Box(
boxModifier,
contentAlignment = Alignment.Center,
) {
Text(
"2",
style =
textStyle.copy(
lineHeightStyle =
LineHeightStyle(
LineHeightStyle.Alignment.Center,
LineHeightStyle.Trim.Both
)
),
color = Color.White,
)
}
}
}
clark
11/17/2023, 6:55 PMjw
11/17/2023, 7:04 PMjw
11/17/2023, 7:05 PMclark
11/17/2023, 7:06 PMjw
11/17/2023, 7:06 PMclark
11/17/2023, 7:08 PMjw
11/17/2023, 7:10 PMfrancisco
11/17/2023, 7:58 PMLineHeightStyle.Trim.Both
to LineHeightStyle.Trim.None
it should add equal top and bottom padding to the text, how does that look? does it fix your issue?clark
11/17/2023, 8:08 PMTrim.Both
and Trim.None
francisco
11/17/2023, 9:52 PMfrancisco
11/17/2023, 9:52 PMclark
11/17/2023, 10:53 PMclark
11/17/2023, 10:54 PMfrancisco
11/17/2023, 10:56 PMfrancisco
11/17/2023, 10:56 PMascii
11/18/2023, 4:45 AM