0

Recently, I have switched to Jetpack Compose, I see that it's very convenient and faster when building UI with Compose. But I find it difficult and confusing to decide which style of Text should use When using MaterialTheme?

For example, when I write code like this

Text(
    text = "Email",
    style = MaterialTheme.typography.,
)

It is difficult and takes a lot of time to decide which typography I should use. It should be bodyMedium, or bodySmall or labelMedium, or titleMedium or any else

Must I use MaterialTheme.typography when using Jetpack Compose, or are there any alternatives?

1 Answer 1

1

You can define your custom TextStyle, and use it. I will explain step by step.

internal val familyHelveticaNeue = FontFamily(
    Font(R.font.helvetic_neue, FontWeight.Normal),
)

fun getHelveticaTextStyle(fontSize: TextUnit, fontFamily: FontFamily) = TextStyle(
    fontFamily = fontFamily,
    fontSize =  fontSize,
//    lineHeight = 24.sp,
//    letterSpacing = 0.5.sp
)

For example, if you have downloaded the helvetica_neue font from the web. You can import the .ttf file inside the font folder (res/font)

Now, the next step will be

Text(
    text = text, style = getHelveticaTextStyle(fontSize = 20.sp, fontFamily = familyHelveticaNeue), color = color)

Not the answer you're looking for? Browse other questions tagged or ask your own question.