androidkotlinandroid-jetpack-composeandroid-fonts

Jetpack Compose Text fontSize behaviour


I'm learning android native with Jetpack Compose, and when I want to assign the font size of a text, I find two ways to do it, with the fontSize property and with the TextStyle.fontSize property, however, the two have a different behavior, the height of the line changes depending on which property I assign:

enter image description here enter image description here

I would like to know why this happens and which of the two options I should continue to use, thank you very much.


Solution

  • The main difference in behavior between fontSize and TextStyle.fontSize is how they interact with other text styling properties. When you set fontSize directly on the Text composable, it applies the size directly without considering any other text styles. On the other hand, when you set TextStyle.fontSize, it applies the font size as part of the text style, which may adjust the height of the line and affect how the text is rendered, especially if you have other text styles applied.

    In general, if you only want to set the font size without any additional styling, using the fontSize property directly on the Text composable is the straightforward approach.