How to Use Font-Awesome 5 Icons in React Native App

Font-Awesome 5 Icons in React Native

In this example, we will see how to use font-awesome Icon in React Native. Font Awesome Icons are the web’s most popular icon set and toolkit. Font Awesome is a robust icon set that contains scalable vector icons. It has a PRO edition with 8000+ icons, and a FREE edition with 2000+ icons. There are many important types of icons that come with Font Awesome like social media, UI/mobile-related, etc.

Benefits

Using font-icons in React Native is very beneficial like

  • No bloatware, one package with one icon set, nothing more nothing less
  • Full set of FontAwesome Icons properly updated
  • Insanely fast with a minimal memory footprint
  • Uses the OS to render icons, for best in class performance

Why Font-Awesome Rendering is fast?

This package uses the Text element to render Icons. The Text element delegates to the OS the render process of the icons based on the Font file. Both IOS and Android render fonts amazingly fast with little memory overhead. In essence FontAwesome.ttf will be used by the OS to render icons and will benefit of years of native software improvement as well as hardware acceleration.

Can Import Like Website?

If you are from a web designing background or worked on some level of HTML designing you must head about the Font-Awesome and also have used the same. For the web, it is very easy to import font-awesome, you just need to import the font-awesome from CDN and you are good to go.

In the case of React Native you have no option to import the font-awesome in the way you do in HTML. We will use react-native-fontawesome library to integrate font-awesome icons in our React Native app. It is very cool and super easy to integrate into the library. You just need to follow the below steps.

Use of Font-Awesome Icon in React Native

Using Font-Awesome Icon is very easy you just need to import the components directly from react-native-fontawesome

//Import FontAwesome Component
//Import SolidIcons to access Solid Icons -> https://fontawesome.com/icons?d=gallery&s=solid
//Import BrandIcons to access Solid Icons -> https://fontawesome.com/icons?d=gallery&s=brands
//Import RegularIcons to access Solid Icons -> https://fontawesome.com/icons?d=gallery&s=regular
//Import parseIconFromClassName function to parse any fa fa-icon to use as fontawesome icon
import FontAwesome, {
  SolidIcons,
  RegularIcons,
  BrandIcons,
  parseIconFromClassName,
} from 'react-native-fontawesome';

And once you import the library you can simply use it as given below

const parsedIcon = parseIconFromClassName('fab fa-apple');
<FontAwesome style={styles.iconStyle} icon={SolidIcons.truck} />
<FontAwesome style={styles.iconStyle} icon={RegularIcons.addressBook} />
<FontAwesome style={styles.iconStyle} icon={BrandIcons.android} />
<FontAwesome style={styles.iconStyle} icon={parsedIcon} />

Points to Highlight

If you see we have directly used the FontAwesome component with the custom styling. You can apply styles directly into the FontAwesome RN component by just passing a style as you do in a <Text> component.

You need to pass the icon name which you can get from the Official Site. There are many filters to find the Icon of your need. Use the below URLs to find the Icon for this example:

  1. Solid Icons
  2. Brand Icons
  3. Regular
  4. fa fa icon

Just remember Javascript doesn’t accept hyphens as valid object names hence to use the icon in React Native remove all hyphens and convert the icon name to camel case.

Example: th-large becomes thLarge

You can also parse the name if you want, like:

import { parseIconName } from 'react-native-fontawesome';

const validIcon = parseIconFromClassName('fas fa-chevron-left') // will be parsed to chevronLeft

// So anywhere you would have used Icons.chevronLeft (or some other icon name)
// you can now just use the returned value directly (validIcon in this example).
// The function parseIconName internally returns an BrandIcons[parsedIconName] or SolidIcons[parsedIconName] or RegularIcons[parsedIconName] result.
// So you can use like that:

<FontAwesome style={{fontSize: 32}} icon={validIcon}>

Project Overview

I think this is enough knowledge to start the example. In this example, we will create a single screen which will have different type of Icons from Font-Awesome. It will be a basic screen so nothing much to describe so let’s start with the example directly.

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.

Installation of Dependency

As I said to use FontAwesome component we are going to use react-native-fontawesome package and to do that we need to install that package. To install this open the terminal and jump into your project

cd ProjectName

Run the following command

npm install react-native-fontawesome --save

This command will copy all the dependencies into your node_module directory.

Download Fonts

To use FontAwesome in React Native you need to download the fonts as a TTF file and have to include in your project the same as I have described below.

We are going to use Regular, Solid, and Brand Icons in this example so please click on the below URL to download TTF file for the same.

  1. Brand Icon TTF
  2. Regular Icon TTF
  3. Solid Icon TTF

Importing Font-Awesome Files in Android

To use Fonts-Awesome in Android, you need to create asset/fonts directory in main and have to copy all the 3 font files in it

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

FontAwesomeAndroidFontSetup

Importing Font-Awesome Files in iOS

Please follow the below steps to use Fonts-Awesome icons in iOS

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

FontAwesomeiOSFontSetup1

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”

FontAwesomeiOSFontSetup2

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

FontAwesomeiOSFontSetup3

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.

FontAwesomeiOSFontSetup4

6. After completion, it will look like this

FontAwesomeiOSFontSetup5

Code to use Font Awesome Icon in React Native

If you have done all the above-expected steps then you can move to the code and can play with FontAwesome Icons.

App.js

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

/* This is how to use Font-Awesome 5 icons in React Native app */
/* https://aboutreact.com/react-native-fontawesome/ */

//Import React
import React from 'react';

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

//Import FontAwesome Component
//Import SolidIcons to access Solid Icons -> https://fontawesome.com/icons?d=gallery&s=solid
//Import BrandIcons to access Solid Icons -> https://fontawesome.com/icons?d=gallery&s=brands
//Import RegularIcons to access Solid Icons -> https://fontawesome.com/icons?d=gallery&s=regular
//Import parseIconFromClassName function to parse any fa fa-icon to use as fontawesome icon
import FontAwesome, {
  SolidIcons,
  RegularIcons,
  BrandIcons,
  parseIconFromClassName,
} from 'react-native-fontawesome';

const App = () => {
  //Parsing a random fa-apple to use it as FontAwesome Icon
  const parsedIcon = parseIconFromClassName('fab fa-apple');

  return (
    <View style={styles.container}>
      <Text style={{ fontSize: 30, textAlign: 'center' }}>
        Example to use{'\n'}Font Awesome Icons{'\n'}in React Native App
      </Text>
      <Text
        style={{
          fontSize: 25,
          marginTop: 20,
          marginBottom: 30,
          textAlign: 'center',
        }}>
        www.aboutreact.com
      </Text>
      <FontAwesome style={styles.iconStyle} icon={SolidIcons.truck} />
      <Text style={styles.textStyle}>Solid Icon</Text>
      <FontAwesome style={styles.iconStyle} icon={RegularIcons.addressBook} />
      <Text style={styles.textStyle}>Regular Icon</Text>
      <FontAwesome style={styles.iconStyle} icon={BrandIcons.android} />
      <Text style={styles.textStyle}>Brand Icon</Text>
      <FontAwesome style={styles.iconStyle} icon={parsedIcon} />
      <Text style={styles.textStyle}>Parsed Icon from class name:</Text>
      <Text style={styles.textStyle}>'fab fa-apple'</Text>
    </View>
  );
};

export default App;

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF',
    flexDirection: 'column',
  },
  iconStyle: {
    fontSize: 40,
    marginTop: 30,
    color: 'black'
  },
  textStyle: {
    marginTop: 5,
    color: 'black'
  }
});

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
react-native run-ios (macOS only).

Output Screenshots

Android

Font Awesome Example Android

IOS

Font Awesome Example IOS 1    Font Awesome Example IOS 2

That was how to use font-awesome 5 icons in the React Native app. 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. 🙂


Related Posts

Leave a Comment

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