Custom Font loading in KMP-Compose Multiplatform.

Ishrat khan
1 min readMay 30, 2023

Loading fonts in KMP is not yet supported officially, but google gives some APIs to load fonts on iOS , Desktop and Android separately.

This is what I did, I used those APIs and combined them with a common function to get FontResources.

Let’s start with the code,

Please keep your fonts in commonMain/resources/font folder.

I created three files FontResource.kt for each module Common , AndroidMain and iosMain.

commonMain

And the file for iosMain is

iosMain

And androidMain module file should be like this

androidMain

This part I have to explain, because here we are manually finding fonts, may be any reader might have better suggestion to remove this manual work, but for now this is how it works.

And the last most important part is

Add these lines to your shareGradle.ktx

sourceSets["main"].res.srcDirs("src/commonMain/resources", "src/androidMain/resources")
sourceSets["main"].resources.srcDirs("src/commonMain/resources")

Note : Feel free to comment if you have any idea to remove that androidMain manual update of Font Resources.

Thanks for reading.

--

--