Skip to main content

How to use custom font family in react-native app ?

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
module.exports = {
  project: {
    ios: {},
    android: {}, // grouped into "project"
  },
  assets: ["./assets/fonts/"], // stays the same
};
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

"rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
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”
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  },
  welcome: {
    fontSize: 20,
    textAlign: "center",
    margin: 10,
    fontFamily: "SFUIDisplay-Bold"
  },
  instructions: {
    textAlign: "center",
    color: "#333333",
    marginBottom: 5,
    fontFamily: "SFUIDisplay-Semibold"
  }
});
Side By Side Comparison After adding “fontFamily” in style

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

Reloading UITableView while Animating Scroll in iOS 11

Reloading UITableView while Animating Scroll Calling  reloadData  on  UITableView  may not be the most efficient way to update your cells, but sometimes it’s easier to ensure the data you are storing is in sync with what your  UITableView  is showing. In iOS 10  reloadData  could be called at any time and it would not affect the scrolling UI of  UITableView . However, in iOS 11 calling  reloadData  while your  UITableView  is animating scrolling causes the  UITableView  to stop its scroll animation and not complete. We noticed this is only true for scroll animations triggered via one of the  UITableView  methods (such as  scrollToRow(at:at:animated:) ) and not for scroll animations caused by user interaction. This can be an issue when server responses trigger a  reloadData  call since they can happen at any moment, possibly when scroll animation is occurring. Example of s...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...

Xcode & Instruments: Measuring Launch time, CPU Usage, Memory Leaks, Energy Impact and Frame Rate

When you’re developing applications for modern mobile devices, it’s vital that you consider the performance footprint that it has on older devices and in less than ideal network conditions. Fortunately Apple provides several powerful tools that enable Engineers to measure, investigate and understand the different performance characteristics of an application running on an iOS device. Recently I spent some time with these tools working to better understand the performance characteristics of an eCommerce application and finding ways that we can optimise the experience for our users. We realised that applications that are increasingly performance intensive, consume excessive amounts of memory, drain battery life and feel uncomfortably slow are less likely to retain users. With the release of iOS 12.0 it’s easier than ever for users to find applications that are consuming the most of their device’s finite amount of resources. Users can now make informed decisions abou...