Custom Fonts in React Native for Android and iOS

React Native Custom Fonts

This is an example to use Custom Fonts in React Native for Android and iOS. We will see how to use a custom font in React Native? Where can we find custom fonts? Steps to download custom fonts, and import in Android and iOS, and at last the code sample which will give you a perfect idea about the use of the custom font in React Native.

In the current scenario fonts and Icons have the same importance in the app. If you are planning for the app with beautiful Icons then you also need beautiful fonts to make it perfect. I personally observed many applications using custom fonts nowadays. If you also want to use the custom font in your React Native app then you can continue with this example.

Using custom fonts in React native is very easy. You do not need any external library for it, just add the fonts in your Android and iOS projects and you are good to go.

How to use custom fonts in React Native

As I said, to use the custom fonts in React Native you need to add fonts in Android and iOS projects (To add the custom fonts steps are mentioned below) and after adding the custom font in the project you can directly set the font using fontFamily prop in Style.

Remember you need to import the font before using it in fontFamily

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF',
    flexDirection: 'column',
  },
  textLargeStyle: {
    fontSize: 40,
    textAlign: 'center',
    // Below is the custom font
    fontFamily: 'DancingScript-Bold',
  },
  textSmallStyle: {
    fontSize: 35,
    marginTop: 20,
    marginBottom: 30,
    textAlign: 'center',
    // Below is the custom font
    fontFamily: 'DancingScript-Regular',
  },
});

Where Can I Found the Custom Fonts?

For those who have the same question in their mind, You can get lots of different fonts from Google Font’s Official Site. Just visit the site and choose the font you like.

Project Overview

In this example, We will have two text components and we will apply the different custom fonts on it so that we can understand how it works. The main thing in this example is to download the font and import them in the Android and iOS projects which are explained below. So let’s get started with the steps to apply custom font in React Native.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Download Fonts

First of all download the font that you want to use in React Native. There are many custom fonts on the Google Fonts site but for this example, I am using

It is a very designer font and you will be able to find the difference in the fonts easily after the completion of the example.

Click the link to go to the download site and follow the steps

1. After opening the URL you will see some sample text with the custom font applied. Click on the Select this syle

CustomFontDownload1

2. After clicking on the Select this Style. You can click on Download Family

CustomFontDownload2

 

3. This will download zip with fonts in it. In simple language, fonts are some ttf file which you can find in static directory after unzipping the downloaded zip

CustomFontDownload3

Importing Custom Font Files in Android

To use Custom Fonts in Android, you need to create asset/fonts directory in projectname/android/app/src/main and have to copy all the font files in it

projectname/android/app/src/main/assets/fonts

CustomFontAndroidSetup

Importing Custom Font Files in iOS

Please follow the below steps to use Custom Fonts icons in iOS

1. Create a fonts directory in the iOS project and copy all the font files there

CustomFontiOSSetup1

2. Now open the project YourProject -> ios -> YourProject.xcworkspace in Xcode.

3. After opening the project in Xcode click on the project from the left sidebar to open the options and select Add Files to “YourProjectName”

CustomFontiOSSetup2

4. Select the fonts directory which you have created. Remember to select Create Folder references from below and click Add

CustomFontiOSSetup3

5. Now click the project name on the left top, and select the project name on TARGETS. Click the Info tab on the top menu to see Info.plist and add Fonts provided by application and font files to Info.plist.

CustomFontiOSSetup4
If you were an iOS developer or have knowledge about info.plist then you can add following lines directly into info.plist but if you performed the above action then you don’t worry about this as this has been already added by XCode.

<key>UIAppFonts</key>
<array>
    <string>fonts/DancingScript-Medium.ttf</string>
    <string>fonts/DancingScript-Bold.ttf</string>
    <string>fonts/DancingScript-Regular.ttf</string>
    <string>fonts/DancingScript-SemiBold.ttf</string>
</array>

6. After completion, it will look like this

CustomFontiOSSetup5

Code to Use Custom Fonts in React Native

If you have done all the above-expected steps then you can move to the code and can use the custom fonts.

Jump into your project

cd ProjectName

App.js

Open App.js in any code editor and replace the code with the following code

// Use of Custom Fonts in React Native for iOS & Android
// https://aboutreact.com/custom-fonts-in-react-native/

// Import React
import React from 'react';

// Import required component
import {
  SafeAreaView,
  StyleSheet,
  Text,
  View
} from 'react-native';

const App = () => {
  return (
    <SafeAreaView style={{flex: 1}}>
      <View style={styles.container}>
        <Text style={styles.textLargeStyle}>
          Use of Custom Font in React Native App
        </Text>
        <Text style={styles.textSmallStyle}>
          www.aboutreact.com
        </Text>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF',
    flexDirection: 'column',
  },
  textLargeStyle: {
    fontSize: 40,
    textAlign: 'center',
    // Below is the custom font
    fontFamily: 'DancingScript-Bold',
  },
  textSmallStyle: {
    fontSize: 35,
    marginTop: 20,
    marginBottom: 30,
    textAlign: 'center',
    // Below is the custom font
    fontFamily: 'DancingScript-Regular',
  },
});

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

To run the project on an Android Virtual Device or on real debugging device

react-native run-android

or on the iOS Simulator by running (macOS only)

react-native run-ios

Download Source Code

Output Screenshots

CustomFontAndroidSetup 

This is how you can use custom fonts in React Native for Android and iOS both. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

I hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.