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 Add/ Remove 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

Master Map & Filter, Javascript’s Most Powerful Array Functions

Master Map & Filter, Javascript’s Most Powerful Array Functions Learn how Array.map and Array.filter work by writing them yourself This article is for those who have written a  for  loop before, but don’t quite understand how  Array.map  or  Array.filter  work. You should also be able to write a basic function. By the end of this, you’ll have a complete understanding of both functions, because you’ll have seen how they’re written. Array.map Array.map  is meant to transform one array into another by performing some operation on each of its values. The original array is left untouched and the function returns a new, transformed array. For example, say we have an array of numbers and we want to  multiply each number by three . We also don’t want to change the original array. To do this without  Array.map , we can use a standard for-loop. for-loop var originalArr = [1, 2, 3, 4, 5]; var newArr = []; for(var i = 0; i < originalArr.length; i+...

Integrate the FaceID or TouchID Authentication in iOS Apps - Swift

FaceID or TouchID Authentication Dear Folks, today I am going to explain the proper way to integrate the Face ID or Touch ID in iOS App. Face ID and Touch ID are secure, familiar authentication methods that people trust. Follow below steps: Step 1: Add NSFaceIDUsageDescription Key in info.plist file <key>NSFaceIDUsageDescription</key> <string>This app requires Face ID permission to authenticate using Face recognition.</string> Step 2: Create a swift file with any name but it would be better to related to its functionality, Like BioMetrixFile.swift Step 3: In BioMetrixFile write below codes and follow instructions. But before proceeding further import LocalAuthentication class in BioMetrixFile. Now, create typealias for success or failure blocks to well manage Authentication Success or Failure. public typealias AuthenticationSuccess = (() -> ()) public typealias AuthenticationFailure = ((AuthenticationError) -> ()) Now written all s...

React Native: How To Open App Settings Page

  React Native: How To Open App Settings Page React Native has a Linking module to manage the interaction with other applications in the device. In this article, I will explain how you can open “Application Setting Page” of the device using. Sample Case Before I explain how to do this, I’ll talk about an example case where we might need this: You need to get location permission within the application from a user using an iOS operating system and you asked for permission. But user did not agree to grant this permission the first time. In this case, you cannot ask your user for location permission the second time. In this case, the only thing to do is to direct the user to the settings page of our application and make it manually allow it. Now that we have explained our example situation, we can move on to how we can redirect our user to the “settings page of our application”. Open App Settings Page iOS If our user is using an iOS operating system, we can handle the whole process wit...