https://kotlinlang.org logo
#compose
Title
# compose
d

dan.the.man

10/14/2021, 5:59 PM
Hello, question: I set a custom theme with Typography of my own font
Copy code
@Composable
fun MyTheme(
    content: @Composable () -> Unit
) {
    MaterialTheme(
        typography = Typography(appFontFamily),
        content = content
    )
}
Using this, it correctly applies the font family if I do a normal text, but as soon as I apply a TextStyle to a
Text
, it doesn't apply that default font family anymore
Copy code
val displayJumboBold = TextStyle(
        fontSize = 40.sp,
        fontWeight = FontWeight.Bold,
        lineHeight = 48.sp
    )
I assume this is because the TextStyle defaults to null for font family if I don't explicitly set it, but should it not fall back to the default of the Theme that I've set?
c

Chris Sinco [G]

10/14/2021, 6:28 PM
Yes, fontFamily is a nullable parameter with a default value of
null
- that ends up resolving back to the DefaultFontFamily of the system (in the implementation of TextStyle), and not from the theme. This is intentional since Compose allows you to override styles at that local composition scope, which in this case is
null
.
If you want to override specific TextStyle properties that deviate from the theme typography styles, I’d suggest setting them using their parameter names directly, e.g. fontSize
d

dan.the.man

10/14/2021, 6:33 PM
It doesn't deviate from the theme's typography style, it defaults back to Roboto, instead of the custom one that I set as
appFontFamily
c

Chris Sinco [G]

10/14/2021, 6:33 PM
Right. I assume though you are trying to use
displayJumboBold
but you don’t want to change fontFamily?
d

dan.the.man

10/14/2021, 6:33 PM
Setting the
Text(style = displayJumboBold)
ends up changing the font back as if I never set the default.
Text()
displays correctly with the
appFontFamily
applied
Yeah, I want
displayJumboBold
to not change the font of the
Text
it's applied ot
c

Chris Sinco [G]

10/14/2021, 6:38 PM
I see - I might be missing some details so maybe @Louis Pullen-Freilich [G] can weigh in. But I believe what you are seeing is expected
l

Louis Pullen-Freilich [G]

10/14/2021, 6:54 PM
The default for the theme is just the default for text styles inside
Typography
, there is no way to set a global default font family in this way. Might be easiest to create your own
Text()
implementation that sets it if it is missing from the style
2 Views