Skip to main content

Ultimate guide to use custom fonts in react native

Adding custom fonts in react native project is very simple task with react-native link command but can be tricky if you are a new in react native .
I have decided to write article on all the possible issues which can occur while implementing custom fonts and give you detail information about how it will work.
Following are steps to link fonts(Using React Native Link Command):

1. First step (Set Font Naming For Cross Platform)

First step is to select a font family you want to use in your react native app. I have decided to use “SF UI Display” for my sample test project.

You need to check the file name before using it . Android will use the font name but in IOS it will use the “PostScript name”. It is tricky part let me explain to you.

Suppose i have above font files and i am using “SFUIDisplayBold” in my react native code it will work in ANDROID but not in IOS because the “postscript name” is “SFUIDisplay-Bold” . So for IOS you have to use “SFUIDisplay-Bold” despite of the fact that you have added font file with name “SFUIDisplayBold.otf”
You can check the “postscript name” of a font file in Font book
After renaming following are list of my font files .
Note : Although you can add logic in your code to use “SFUIDisplayBold” for Android and “SFUIDisplay-Bold” for IOS but it is better to rename the font .
Step 2 : Add Fonts to Assets
Next add all the font files you want to us in “assets/fonts” directory (It can vary)
Step 3 : Define assets directory
if React Native Version ≥ 0.60 (Detail)
Create File “react-native.config.js” and add following code
if React Native Version < 0.60
You need to tell react native where our custom fonts are located . Adding the following lines in your package.json
Path can vary. if you have “src” folder and you want to put assets in “src” folder then path will be “.src/assets/fonts/”
Step 4 : Link assets using react native link
Run in terminal
$ react-native link
link command will links fonts in Info.plst for IOS and creates fonts directory (android/app/src/main/assets/fonts) for Android, where copies your fonts
Step 5 : Use font in React Native Styles
Finally you can use font in styles by adding “fontFamily”
Side By Side Comparison After adding “fontFamily”

Manually AddRemove Fonts

After running react-native link you should see something like this in your terminal
This command will done all the hard work for you. Create “Resources” folder in Xcode project ,copy files in “Resources” folder , add fonts in “info.plist” in , creates fonts directory (android/app/src/main/assets/fonts) for Android
There is no command for Unlink(Remove) assets you have to do it manually . So if you want to Unlink(Remove) fonts or manually add fonts without react native link command following are steps
Step 1 : Add/Remove fonts to android project in following directory
android/app/src/main/assets/
Step 2 : Add/Remove fonts to “Resources” folder in Xcode project
Note : Just Drag the fonts in Xcode “Resources” folder and check “Copy items if needed” . Add to target should be your project .For Detail see link
if files are copied correctly you can see the files in “Copy bundle resources” in “Build phases” tab
In case of remove please remove it from bundle resources
Step 3 : Add/Remove fonts in info.plist files in Xcode project with key “Fonts provided by application”
You can see my demo project code on Github . I have tried by best to give you detail information about how to use custom fonts . Thanks for reading my article . Please clap if you liked it!

Comments

Popular Posts

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...