Set Common Custom Font in KMP Kotlin Compose Multiplatform Text

Today’s tutorial will teach us how to apply a custom font in KMP Kotlin Compose Multiplatform using CommonMain Shared resources. This is a step-by-step tutorial guide for both Android & iOS applications. We will use a single font family TTF file, which will be shared throughout the application.

Set Common Custom Font in KMP Kotlin Compose Multiplatform Text

Set Common Custom Font in KMP Kotlin Compose Multiplatform Text

In Kotlin Compose Multiplatform, we can use Shared resources such as images, Text String files, and fonts. These static resource files are shared across the app, and we can directly use them using the common Main shared code. We must write a single code that will work for Android and iOS. As we all know, There are a ton of Font styles available on the internet, and customized font styles are a great way to enhance the design of our application. To set up a custom font in our KMP app, we have to make a few changes to our project. So let’s get started.

1. Open your KMP Compose Multiplatform project and Goto

composeApp -> commonMain -> composeResource.

Common Mail compose resource in KMP project

2. In composeResource, create a folder named the font. Then, put your custom font TTF files in that folder. As you have seen in the below screenshot.

Common main font folder

3. Now you have to Rebuild the project. This is a mandatory step.

Rebuild Android Studio project.

4. Applying custom font on Text using the FontFamily property of text. The import of Res will be automatically generated after rebuilding the project.

Complete source code for App.kt file:

Screenshot in Android device:

Set Common Custom Font in KMP Kotlin Compose Multiplatform Text

Screenshot in iOS device:

KMP iOS Font

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *