I don't want to override TextStyles h1, h2, button...
# compose
p
I don't want to override TextStyles h1, h2, button from Typography and wish to create my own custom TextStyle and set them in custom Typography class. But I can't find a way to set custom TextStyle in Typography. Can anyone help out here?
1
a
Typography
class is bound to the type scale defined by Material, so it looks closed to adding custom scale categories (and subclassing won’t make things easier unless you constantly cast
MaterialTheme.typography
to your typography subclass). Creating and integrating a custom typography class looks tedious. Tedious at creation because you’ll need to define a custom typography class, your own
CompositionLocal
to hold current typography, a custom theme composable that provides this current typography, …; And tedious on its integration because you should probably avoid composable functions from the Material library, such as
Text
or
Button
, to avoid unexpected side effects or even crashes. So, unless you need to build a solid design system that differs from Material guidelines, you could just define your custom text styles in variables inside `Type.kt`:
Copy code
val myCustomTextStyle = TextStyle(
    fontFamily = FontFamily(Font(R.font.helvetica)),
    fontSize = 18.sp,
    baselineShift = BaselineShift(-0.16f),
    textAlign = TextAlign.Center
)
And then apply it to a Text composable:
Copy code
Text(
    style = myCustomTextStyle,
    text = "A text"
)