https://kotlinlang.org logo
#compose-android
Title
# compose-android
k

Kevin Worth

04/28/2023, 2:45 PM
TLDR: does android/compose/material have "auto" line height? When overriding a font style to set a font size, what happens if we don't set line height? Is it automatically adjusted? Or does it keep the default value? Does the answer differ for either of the following: • Using
.copy
Copy code
// don't make any change in Type.kt
...
Text(
    text = ...
    style = MaterialTheme.typography.displayLarge.copy(fontSize = 50.sp)
)

// Will this have a line height of 64, or will line height be "auto"?
• Override in
Type.kt
Copy code
displayMedium = TextStyle(
        fontFamily = FontFamily.Default,
        fontWeight = FontWeight.SemiBold
        fontSize = 40.sp,
//      don't set lineHeight
//      don't set letterSpacing
    ),

...
Text(
    text = ...,
    MaterialTheme.typography.displayMedium
)

// Will this have a line height of 52, or something like Unspecified, which then means "auto"? Or...?
Our designers are taking advantage of Figma's ability to set line height to "auto"(which ends up being odd values like 51.74) and they ask, "Wait, you mean Android doesn't have auto line height?" So, does it?
f

Filip Wiesner

04/28/2023, 2:49 PM
TLDR: No 😞 "Autosize Text" is in the roadmap (been there for a while now)
a

Albert Chang

04/28/2023, 3:29 PM
Of course there is auto line height. Auto size is totally different thing. Basically
TextUnit.Unspecified
means auto line height, and if the text style had a specified line height, copying it will not change the line height unless you set
lineHeight = TextUnit.Unspecified
.
However note that line height on Android is not necessarily equal to that in Figma.
f

Filip Wiesner

04/28/2023, 3:47 PM
Ouch, that's what I get for reading only the TLDR (and not even that)
k

Kevin Worth

04/28/2023, 3:57 PM
@Albert Chang yup, they tell us they don't care about line height. We will use Roboto, set the font size according to what's in our figma designs, and let Unspecified/"auto" take care if it. Thank you!
c

Chris Sinco [G]

04/28/2023, 4:11 PM
What can still happen though is that currently
includeFontPadding
is set to
true
by default in Compose for historical reasons. That will change in the future, which should make the auto line heights more consistent with Figma. If you don’t set it to
false
, your designers may find it doesn’t match their specs.
k

Kevin Worth

04/28/2023, 4:46 PM
Hey guys, is the same true for
letterSpacing
? Looks like I can set that to
Unspecified
, and then it will use an auto
letterSpacing
for Roboto?
c

Chris Sinco [G]

04/28/2023, 4:48 PM
I believe so, but @Siyamed can double confirm
s

Siyamed

04/28/2023, 5:09 PM
I dont know what auto letterspacing is. But yes set it to unspecified it will render as no letterspacing set.
Unspecified means use the defaults. /Line height default is not 1.
k

Kevin Worth

04/28/2023, 5:32 PM
Thanks, forgive my ignorance in making guesses like whether auto letter spacing is a thing or not (now that I think about it further, I think I get it)
@Chris Sinco [G]
includeFontPadding
is new to me and I've now found it as a parameter of
PlatformTextStyle
which is deprecated and says
Copy code
'constructor PlatformTextStyle(Boolean = ...)' is deprecated. includeFontPadding was added to Android in order to prevent clipping issues on tall scripts. However that issue has been fixed since Android 28. Compose backports the fix for Android versions prior to Android 28. Therefore the original reason why includeFontPadding was needed is invalid on Compose.This configuration was added for migration of the apps in case some code or design was relying includeFontPadding=true behavior; and will be removed.
So, sounds like we no longer need to worry about it? Or am I missing something?
c

Chris Sinco [G]

04/28/2023, 5:50 PM
You may as it is on by default so line heights might not match up as well as with Figma, since tools like that don't have a concept of fontPadding to turn on. It will be off by default in future versions of Compose so in the future you won't have to worry. It's most noticeable in situations like vertically aligning text and an icon. We have the deprecation notice there so ppl can be sure to delete it in the future.
A lot of apps migrated from Views had not turned it off, so when screens were being compared to Compose, it felt off.
k

Kevin Worth

04/28/2023, 6:06 PM
Ok, so if I'm understanding correctly, I should do this:
Copy code
titleMedium = TextStyle(
        ....
        lineHeight = TextUnit.Unspecified,
        ...
        platformStyle = PlatformTextStyle(
            includeFontPadding = false
        )
    ),
And then we'll keep an eye out for when it is removed, so then we can remove
platformStyle
. Yes?
c

Chris Sinco [G]

04/28/2023, 6:12 PM
Yes - though only if you need it! You may not in every case with Text.
The deprecation notice is a bit confusing at first, which is why we mention this in the Medium article above
Use
PlatformTextStyle
API from
Compose 1.2.0
to configure
includeFontPadding
It is deprecated on purpose to indicate that it is a compatibility API.
j

Joel Denke

04/30/2023, 5:37 AM
IncludeFontPadding = false in Compose 1.4 as default value fyi :)
s

Siyamed

05/01/2023, 1:54 PM
We removed the deprecation a few weeks ago, it will be in the next major release (1.5?) We also plan to make it false by default in a few months (1.6?)
The reason for deprecation was we never wanted to have an API for the behavior, but also not having it caused issues in compatibility with apps that want to use compose.Text and TextView side by side. We decided that the API should stay there for an unknown or unlimited time, and removed the deprecation.
k

Kevin Worth

05/01/2023, 4:19 PM
Sorry, so it's false in 1.4 (as @Joel Denke says) or it will be false in 1.6 (as @Siyamed says)?
j

Joel Denke

05/01/2023, 4:32 PM
Not sure, I read in blog post from Google earlier it should be default in 1.4. however not sure now (Google maybe changed their mind). Easy to check source code for it, I just dont have any computer available at the moment. Sounds like @Siyamed working with this, would trust that answer :)
300 Views