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

74 Views Asked by At

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?

2

There are 2 best solutions below

2
ucMedia On BEST ANSWER

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
)
0
shaktiman_droid On

There is now official experimental support for loading fonts in compuse multiplatform. Follow the images and resources documentation.

If official support is not for your case yet, then the above page also list outs the 3rd party libraries that you can try.

On top of that, also look at this demo project where they update all the latest functionality.

Regarding IntelliJ, everything should be same as long as you are using the correct multiplatform plugins.