how to set specific font family for each locale in jetpack compose

Mohammad

In Jetpack Compose, you can set your specific font by editing the type file:

val MyFont= FontFamily(
    Font(R.font.myfont, FontWeight.Normal)
)

// Set of Material typography styles to start with
val Typography = Typography(
    body1 = TextStyle(
        fontFamily = MyFont,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp
    ),
    button = TextStyle(
        fontFamily = MyFont,
        fontWeight = FontWeight.SemiBold,
        fontSize = 14.sp
    ),
    defaultFontFamily = MyFont
)

But my problem is that I have multiple locales, and I want to set the specific font for each one.

Before using Compose, my approach was to create a style.xml file in each language values folder and edit the style.xml in a way that changes the font family. but this approach won't work when using Compose.

So how can I have a different font family for each locale?

Liem Vo

If you want to achieve this you need to follow some steps as below:

  1. These fonts should be the same name and put in the different font locale folder. Put fonts in different folder

  2. Define FontFamily with that font name

    val myFontFamily = FontFamily(
        Font(R.font.lato_light, FontWeight.Light),
        Font(R.font.lato_regular, FontWeight.Normal),
        Font(R.font.lato_italic, FontWeight.Normal, FontStyle.Italic),
        Font(R.font.lato_regular, FontWeight.Medium),
        Font(R.font.lato_bold, FontWeight.Bold)
    )
    
  3. Use in your Text

    val text = "Hello Android Developers"
    Column(Modifier.fillMaxWidth().padding(16.dp)) {
        Text(text, fontFamily = myFontFamily, fontWeight = FontWeight.Light)
        Text(text, fontFamily = myFontFamily, fontWeight = FontWeight.Medium)
        Text(text, fontFamily = myFontFamily, fontStyle = FontStyle.Italic)
    }
    

The result of English and Spanish locale as below: Text will be in different font in different locales (en&es)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to use a specific font from a font family

How to set an alarm with jetpack compose?

Set font-family to specific symbols in CSS

How to change font-family when locale language changes in vuetify

How to set font family but retain fallback stack?

How to set default font family in React Native?

How to set the font family I want in css?

How to correctly set height in TextField in Jetpack Compose?

How to set padding conditionally in jetpack compose

How to set a focused state to a Button with jetpack compose?

How to set a custom Context in Jetpack Compose?

How to set default elevation for ExtendedFloatingActionButton in Jetpack Compose

How to Set Button Height and Width in Jetpack Compose?

How to set itemActiveIndicatorStyle on BottomNavigationView in Jetpack Compose?

How to set font dynamically based on locale in rails?

How to change the input font size in TextField of Android Jetpack Compose

How to change font family + background when hovering over each letter

Locale.getDefault() shows error in jetpack compose

How to use specific member of a font-family in xsl-fo

Set font-size for all elements with a specific font-family with pure css, no Javascript

How to set the font-family as specified when editing post in wordpress?

How to set default font family for entire Android app

how to set the UIFont family and name with display font on the UITableView?

How to set a font family to an entire Word document in Apache POI XWPF

How to set the font-family like a digital clock CSS?

How to set a global/default font-family without universal selectors?

How to Set a Custom Font Family in the Xamarin ActionBar Title?

how to set default font family available in android to action bar title

How to set a font family to "erewhon" when using Latex in matplotlib?