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
// parseIconFromClassName to parse any fa fa-icon into fontawesome
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:
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
becomesthLarge
You can also parse the name if you want, like:
import { parseIconName } from 'react-native-fontawesome';
const validIcon = parseIconFromClassName('fas fa-chevron-left')
/*
Will parsed to fa-chevron-left to chevronLeft,
You can now just use the returned value directly
*/
/*
The function parseIconName internally returns
BrandIcons[parsedIconName] or SolidIcons[parsedIconName]
or RegularIcons[parsedIconName] result.
*/
<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 command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
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.
Importing Font-Awesome Files in Android
To use Fonts-Awesome in Android, you need to create assets/fonts directory in main and have to copy all the 3 font files in it
projectname/android/app/src/main/assets/fonts
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
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”
4. Select the fonts directory which you have created. Remember to select Create Folder references from below and click Add
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.
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/fa-brands-400.ttf</string>
<string>fonts/fa-regular-400.ttf</string>
<string>fonts/fa-solid-900.ttf</string>
</array>
6. After completion, it will look like this
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
// How to Use Font-Awesome 5 Icons in React Native App
// https://aboutreact.com/react-native-fontawesome/
// Import React
import React from 'react';
// Import required component
import {SafeAreaView, StyleSheet, Text, View} 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
*/
// parseIconFromClassName to parse fa fa-icon into fontawesome
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 (
<SafeAreaView style={{flex: 1}}>
<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>
</SafeAreaView>
);
};
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
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
Android
IOS
That was how to use font-awesome 5 icons in the React Native app. If you have any doubts or 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. 🙂
I read this article “https://coolmonktechie.com/react-native-how-to-use-font-awesome-icons-in-react-native/”, same information like your’s, same source code , same order, just one icon is your difference.
So who is copying the Other?
If it is you, Be real to yourself, that is not gonna help you.
If it is not you, then reach out to them for stealing your Original content.
Hehe, There are so many people stealing the content from AboutReact it shows their standard and we can’t say anything as reporting will give then the attention they want and everybody knows hardwork and quality takes time to get famous but with controversy you can get fam within few days.