'How to load Fonts in Jetpack Compose Desktop?
In Jetpack Compose for android you can do this:
val fontFamily = FontFamily(
Font(
resId = R.font.my_font_400_regular,
weight = FontWeight.W400,
style = FontStyle.Normal
),
Font(
resId = R.font.my_font_400_italic,
weight = FontWeight.W400,
style = FontStyle.Italic
)
)
But for Desktop the Filestructure is different and I have no Access to R.font.my_font_400_regular
since 'R' is a Android Resource feature.
Solution 1:[1]
Put your .ttf
font file in the src > main > resources
folder. And then use:
val fontFamily = FontFamily(
Font(
resource = "font.ttf",
weight = FontWeight.W400,
style = FontStyle.Normal
)
)
Solution 2:[2]
Also, if you are using multiple fonts and want them to each be in their own sub-directory in resources like
resources/fonts/example1/
then make sure to include that directory in your 'resource' string in the Font creation.
Font(
resource = "fonts/example1/examplefont_bold.ttf",
weight = FontWeight.Bold,
style = FontStyle.Normal
)
Probably obvious, but just in case.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Saurabh Thorat |
Solution 2 | Alex Johnson |