Prashast Rastogi
05/16/2021, 7:48 AMAlex Barcelo
05/16/2021, 10:47 AMTypography
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`:
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:
Text(
style = myCustomTextStyle,
text = "A text"
)