Hello, question: I set a custom theme with Typogra...
# compose
d
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
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
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
Right. I assume though you are trying to use
displayJumboBold
but you don’t want to change fontFamily?
d
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
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
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