0

I want to set text size as "DP". so I add extension fuction like below

@Composable
fun Dp.toTextDp(): TextUnit = textSp(density = LocalDensity.current)

private fun Dp.textSp(density: Density): TextUnit = with(density) {
    this@textSp.toSp()
}

However I found text line hight issue when change device font size to Huge

Here is my text test code.
there are two text view has different way to set font size first is using TextStyle second is just set font Size.

@Composable
fun TextLineLayout(description: String) {

    Text(
        modifier = Modifier,
        text = description,
        color = Color.Gray,
        style = TextStyle(fontSize = 20.dp.toTextDp())
    )
    Text(
        modifier = Modifier,
        text = description,
        color = Color.Blue,
        fontSize = 20.dp.toTextDp(),
    )
}

and then add preview function with fontScale is 2f

@Preview(name = "font_size", fontScale = 2f)
@Composable
fun TextLinePreview() {
    MaterialTheme {
        TextLineLayoutTest()
    }
}

enter image description here

The Hight of Second Text is not my expected. The reason I wasn't getting the results I was expecting is, I think, is the line height.
At that time, Default lineHeight is lineHeight=24.0.sp
But First TextView has lineHeight=Unspecified because, I think, TextStyle was overrided

Is my analysis correct? So should I set everty text size with TextStyle if I want to set "DP" size?

Chloe
  • 1

0 Answers0