Search code examples
intellij-ideaandroid-jetpack-composedesktopkotlin-multiplatformwordpress-jetpack

How to add a new font family to my project app compose for desktop? -IntelliJ IDEA-


I have a desktop application project using Compose for desktop and I want to use new fonts other than the ones provided automatically, knowing that I work in IntelliJ IDEA.

enter image description here

What is the best way to solve this problem because the file structure is not the same as the file structure in Android Studio?


Solution

  • 1. place the fonts in your project structure, Create a fonts directory inside your project's resources folder and place the font files within it.
    The typical path is src/main/resources/fonts.

    2. Create a FontFamily instance, and use the relative paths from the resources folder. For example :

    val Ubuntu = FontFamily(
        Font(resource = "fonts/ubunturegular.ttf",FontWeight.Normal),
        Font(resource = "fonts/ubuntubold.ttf",FontWeight.Bold),
        Font(resource = "fonts/ubuntulight.ttf",FontWeight.Light),
        //... desired weights and styles, (available options in FontWeight and FontStyle)
    )
    

    3. Pass the FontFamily instance to the fontFamily parameter of various text-related Compose components, such as Text, Button...

    Text(
        text = "This is Ubuntu Font!",
        fontFamily = Ubuntu,
        fontWeight = FontWeight.Bold // optional
    )