How to use Font Awesome in an Ionic Application

2017-10-18

There is an element of confusion when it comes to use FontAwesome in an Ionic Application. To include this fonts library in Ionic needs a bit of configuration and this step by step guide will provide just that.

Bootstrap an Ionic Application

Create a new Ionic application or you can follow these steps to include FontAwesome in your Ionic application. Nothing will break down.

1$ ionic start ionic-use-fontawesome

Now, install the FontAwesome package from npm and save it in the app's dependencies:

1$ npm install font-awesome --save

Type the above in the root folder of your app. This will add the font-awesome folder under node_modules. The --save option will include the package inside the dependencies section of package.json file.

Include the fonts

Now wander to the src/index.html file and add the following link tag just above build/main.css:

1<link rel="stylesheet" href="assets/css/font-awesome.min.css" />
2<link href="build/main.css" rel="stylesheet" />

Add a Custom Copy Script

Ionic includes a copy script file that is called during the build process when the command ionic serve executes and it is responsible for moving specific and required resources from node_modules to the www folder such as ionicons.

The location of this script file is under node_modules folder:

Copy this file and paste in inside a new directory called config under the root directory of your Ionic application. Open the file and add fontawesome entries at the bottom of this file.

1copyFontawesomeFonts: {
2 src: ['{{ROOT}}/node_modules/font-awesome/fonts/**/*'],
3 dest: '{{WWW}}/assets/fonts'
4 },
5 copyFontawesomeCss: {
6 src: ['{{ROOT}}/node_modules/font-awesome/css/font-awesome.min.css'],
7 dest: '{{WWW}}/assets/css'
8 }

Include Custom Copy Script in package.json

We must include this custom script inside our package.json file to override the one where we copied the original file from. Open the package.json file and add this json code:

1"config": {
2 "ionic_copy": "./config/copy.config.js"
3 }

That’s it in the configuration part.

Use FontAwesome

To implement FontAwesome in this or any Ionic application after the above configuration, we try adding some fonts on our pre-generated home page.

1<ion-header>
2 <ion-navbar color="secondary">
3 <ion-title>
4 FontAwesome Icons
5 </ion-title>
6 </ion-navbar>
7</ion-header>
8
9<ion-content padding>
10 <ion-list>
11 <ion-item>
12 <i class="fa fa-battery-empty" style="color: red;"></i> Battery Level
13 Empty
14 </ion-item>
15 <ion-item> <i class="fa fa-battery-1"></i> Battery Level 1 </ion-item>
16 <ion-item> <i class="fa fa-battery-2"></i> Battery Level 2 </ion-item>
17 <ion-item> <i class="fa fa-battery-3"></i> Battery Level 3 </ion-item>
18 <ion-item>
19 <i class="fa fa-battery-4" style="color: green;"></i> Battery Level 4
20 </ion-item>
21 </ion-list>
22</ion-content>

There are more than 675 icons available in FontAwesome and I think a combination of both Ionicons and FontAwesome will be enough for some the applications out there. You can even style them, change their font-size and color as per your needs.

Full source code at this Github Repository

Originally Published at Hackernoon.com

I'm Aman working as an independent fullstack developer with technologies such as Node.js, ReactJS, and React Native. I try to document and write tutorials to help JavaScript, Web and Mobile developers.