https://kotlinlang.org logo
Title
v

vanshg

04/12/2023, 4:45 AM
Hi, I want to define a custom
Typography
where the only change I want to make different from default is a different Font Family. I'm currently achieving this by copying each text style within MaterialTheme.typopography -- is this the best way to do it? Is there something more concise instead?
val MyCustomFontTypography: Typography
    @Composable get() = Typography(
        displayLarge = MaterialTheme.typography.displayLarge.copy(fontFamily = epilogue),
        displayMedium = MaterialTheme.typography.displayMedium.copy(fontFamily = epilogue),
        displaySmall = MaterialTheme.typography.displaySmall.copy(fontFamily = epilogue),
        headlineLarge = MaterialTheme.typography.headlineLarge.copy(fontFamily = epilogue),
        headlineMedium = MaterialTheme.typography.headlineMedium.copy(fontFamily = epilogue),
        headlineSmall = MaterialTheme.typography.headlineSmall.copy(fontFamily = epilogue),
        titleLarge = MaterialTheme.typography.titleLarge.copy(fontFamily = epilogue),
        titleMedium = MaterialTheme.typography.titleMedium.copy(fontFamily = epilogue),
        titleSmall = MaterialTheme.typography.titleSmall.copy(fontFamily = epilogue),
        bodyLarge = MaterialTheme.typography.bodyLarge.copy(fontFamily = epilogue),
        bodyMedium = MaterialTheme.typography.bodyMedium.copy(fontFamily = epilogue),
        bodySmall = MaterialTheme.typography.bodySmall.copy(fontFamily = epilogue),
        labelLarge = MaterialTheme.typography.labelLarge.copy(fontFamily = epilogue),
        labelMedium = MaterialTheme.typography.labelMedium.copy(fontFamily = epilogue),
        labelSmall = MaterialTheme.typography.labelSmall.copy(fontFamily = epilogue),
    )
y

yschimke

04/12/2023, 5:37 AM
I've written this code before, so I think it's close. My only suggestion is a
Typography.copy(TextStyle.() -> TextStyle)
extension method that applies to all styles in one place so you only need to write it once.
a

Albert Chang

04/12/2023, 5:52 AM
It doesn't have to be a composable function. You can just use:
Typography().run {
    Typography(
        displayLarge = displayLarge.copy(fontFamily = epilogue),
        ...
    )
}
s

Simon Hardt

04/12/2023, 7:20 AM
maybe have a look at material's
withDefaultFontFamily()
in
androidx.compose.material.Typography.kt
- they do the same thing
m

maiatoday

04/12/2023, 7:46 AM
There is a default font family in m2 but not in m3 afaik
v

vanshg

04/12/2023, 4:25 PM
@Albert Chang Ah, that's a great suggestion