Using `AnnotatedString` with `SpanStyle`'s definin...
# compose
h
Using
AnnotatedString
with `SpanStyle`'s defining large font sizes the space between lines becomes smaller and smaller. It seems to be fine until font sizes larger then 24 sp. Anyone noticed this too? What could be wrong?
t
see https://app.slack.com/client/T09229ZC6/CJLTWPH7S/thread/CJLTWPH7S-1655996484.238529 you need to also increase lineHeight. By default they are not linked.
h
Interesting, I would have expected the lineHeight to be based on the font size by default. SpanStyle doesn't support lineHeight, so we'll have to add a ParagraphStyle too.
t
Yes I would have though too, but seems it's not currently. Ping @Halil Ozercan to see if something came up from that previous interaction.
a
I think you can use
em
instead of
sp
to make it based on font size.
h
Unfortunately we still will have to wrap it in a ParagraphStyle in AnnotatedString. I'm really surprised at the default behavior. That doesn't seem correct to me.
a
The default line height of
TextStyle
is
TtextUnit.Unspecified
, which means the line height will depend on the font size. Your problem is because you are specifying line height in
sp
, which is an absolute unit.
h
Can you please share a minimum repro? There are multiple things that affect the line height, so it's difficult to guess what could be going wrong.
h
We are already defining the lineHeight in the TextStyle but when using this in AnnotatedString, the lineHeight goes missing using the toSpanStyle extension because the lineHeight can only be defined in a ParagraphStyle.
h
Line height won't be missing just because it's not in SpanStyle. Line height from TextStyle will be applied to the Text as a whole. However, if you specify a line height in
sp
, then the relativity of it to fontSize will be lost. You need to use
em
for that.
h
That still doesn't work for us. If the AnnotatedString contains multiple SpanStyles with different font sizes and the TextField style consists of a TextStyle with a lineHeight of 2 em, the spaces between lines are still not what we would expect (too small for large sizes, too large for small sizes). I will create an example...
Copy code
Text(
    text = buildAnnotatedString {
        withStyle(SpanStyle(fontSize = 36.sp)) {
            append("Some text with a font Size of 36.sp and long enough to cause a new line")
        }
        append("\n\n")
        withStyle(SpanStyle(fontSize = 14.sp)) {
            append("Some text with a font Size of 14.sp and long enough to cause a new line")
        }
    },
    modifier = Modifier.fillMaxSize(),
    style = TextStyle.Default.copy(lineHeight = 2.em)
)
gives the following result
@Johnny @Artur Schwarz fyi
@Halil Ozercan But setting it explicitly to TextUnit.Unspecified seems to fix the problem, although our custom line heights are lost in that context.