57

I want to set fontFamily to roboto thin of my toolbar title.

I have added roboto thin ttf in assets/fonts folder of my android project, however it seems that it is creating issues while running app. I am getting this issue while running

react-native start

ERROR  EPERM: operation not permitted, lstat 'E:\Myntra\android\app\build\gener
ated\source\r\debug\android\support\v7\appcompat'
{"errno":-4048,"code":"EPERM","syscall":"lstat","path":"E:\\Myntra\\android\\app
\\build\\generated\\source\\r\\debug\\android\\support\\v7\\appcompat"}
Error: EPERM: operation not permitted, lstat 'E:\Myntra\android\app\build\genera
ted\source\r\debug\android\support\v7\appcompat'
    at Error (native)

When I am removing the font then it is working fine. I am unable to fix this issue. What's the reason?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • 1
    There is step by step process explained at https://stackoverflow.com/a/52916277/5519329 – Sateesh Yemireddi Nov 05 '18 at 12:25
  • Note that if you don't want to use the Postscript name (e.g. `FontName-Bold` which will cause **typefont modifiers** such as `fontWeight` to mismatch the font on Android, in addition to being inconsistent with how fonts are handled on iOS, you should follow up this guide: https://stackoverflow.com/a/70247374/2779871 – Jules Sam. Randolph Dec 17 '21 at 15:45

18 Answers18

106

UPDATE

Many answers are here for adding custom font in react-native for version < 0.60.

For those who are using react-native version > 0.60 , 'rnpm' is deprecated and custom fonts will not work.

Now, in order to add custom font in react-native version > 0.60 you will have to :

1- Create a file named react-native.config.js in the root folder of your project.

2- add this in that new file

module.exports = {
project: {
    ios: {},
    android: {},
},
assets: ['./assets/fonts']
};

For those running on react-native version < 0.69.x

3- run react-native link command in the root project path.

PS Make sure you have the right path for the fonts folder before running react-native link command


For those running on react-native version >= 0.69.x, Since link is deprecated so react-native link will not work anymore, the command react-native link is replaced by npx react-native-asset.

More info about the release can be seen here: https://github.com/react-native-community/cli/releases/tag/v8.0.0

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
67
  1. Add your fonts file in
  • Project folder/android/app/src/main/assets/fonts/font_name.ttf
  1. Restart the package manager using react-native run-android
  2. Then you can use your font in your style e.g
  • fontFamily: 'font_name'
tamilselvancst
  • 478
  • 1
  • 3
  • 19
Sport
  • 8,570
  • 6
  • 46
  • 65
48

Put all your fonts in you React-Native project directory

./assets/fonts/

Add the following line in your package.json

"rnpm": {
  "assets": ["./assets/fonts"]
}

finally run in the terminal from your project directory

$ react-native link

to use it declare this way in your styles

fontFamily: 'your-font-name without extension'

If your font is Raleway-Bold.ttf then,

fontFamily: 'Raleway-Bold'
Nitin Anand
  • 795
  • 6
  • 9
12

Update:

From the cli docs, "rnpm" is deprecated and support for it will be removed in next major version of the CLI.

Instead, create a react-native.config.js in your project folder

module.exports = {
  assets: ['./assets/fonts'],
};

Put your fonts in ./assets/fonts. Reference your fonts (e.g. McLaren-Regular.ttf) in the styles prop, {fontFamily: 'McLaren-Regular'}. If you're using styled components, then font-family: McLaren-Regular

No linking or legacy build settings needed for either platforms. If that didn't work (sometimes it doesn't for me), run npx react-native link, even if you're using autolinking.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
11

If you're using React Native chances are that you are using Expo as well. If that's the case, then you can load custom fonts using Expo's Font.loadAsync method.

Steps:

  1. Put the downloaded font in the ./assets/fonts directory (if the directory doesn't exist, create it)
  2. From the target component (for example: App.js) load Expo's Font module:

    import { Font } from 'expo'
    
  3. Load the custom font using componentDidMount:

    componentDidMount() {
      Font.loadAsync({
        'Roboto': require('../assets/fonts/Roboto-Regular.ttf'),
      })  
    }
    
  4. Finally, use the style attribute to apply the desired font on a <Text> component:

    <Text style={{fontFamily: 'Roboto', fontSize: 38}}>Wow Such Title</Text>
    
Juan Marco
  • 3,081
  • 2
  • 28
  • 32
  • Due to Expo's constraint of not being able to *fully run offline, then I think your opening statement is a little, er misleading.. I use both Expo & react-native, Expo is great, but simply doesn't have the features for a lot of common use-cases. Nice answer though – Lee Brindley Feb 03 '18 at 19:32
5

STEP 1:

Create a config file at the root of the project named "react-native.config.js"

STEP 2:

Add the following code inside.

 module.exports = {
        project: {
        ios:{},
        android:{}
          },
        assets:['./assets/fonts/'],
    }

STEP 3:

Run the following command:

npx react-native link       (React-native version < 0.69)
npx react-native-asset      (React-native version > 0.69)
Muhammad Haidar
  • 1,541
  • 16
  • 17
2

Adding Custom Font with EXPO

If you're using React Native chances are that you are using Expo as well. If that's the case, then you can load custom fonts using Expo's Font.loadAsync method.

Steps

  1. Move your font to asset/fonts folder
  2. From the target component (for example: App.js) load Expo's Font module:
import { Font } from 'expo'
  1. Set a state
this.state = {
   fontLoaded: false
}
  1. Load the custom font using componentDidMount:
async componentDidMount() {
   await Font.loadAsync({
       'ComicSansBold': require('../assets/fonts/ComicSansMSBold.ttf'),
   })

   this.setState({fontLoaded: true})
}
  1. Finally, use the style attribute to apply the desired font on a component:
{
  this.state.fontLoaded
  ? <Text style={{
    fontSize: 48,
    fontFamily: 'ComicSansBold'
    }}>Glad to Meet You!</Text>
  : null
}

Enjoy Coding....

My Output: How to add custom font in react native

Chhay Rith Hy
  • 1,359
  • 1
  • 7
  • 4
2

RNPM has been merged into React Native core. This means that you don’t need RNPM anymore. So please they don’t want you to use it. Stop using it.

Here are 7 steps broken down to help you set fonts up:

  1. Have your fonts ready, you can download your fonts from GoogleFonts, AdobeFonts, etc. Fonts can be in .ttf, or .otf

  2. Create a configuration file in the root of your project for fonts. Create a file called:

    react-native.config.js

  3. Create the folder to house your fonts. You can create a folder called fonts inside the assets folder.

  4. Paste your .ttf or .otf fonts inside of it.

  5. Write a configuration inside of react-native.config.js file, and paste the following:

    module.exports = { assets: ['./src/assets/fonts'], };

Change the path to the path of the folder housing your fonts.

  1. Now natively set the fonts for Android and IOS. You don’t need to manually do that, just run on your terminal:

    react-native link

  2. Any new fonts you add, make sure you run react-native link again on your terminal to natively set the fonts.

Anayo Oleru
  • 468
  • 5
  • 8
1

@nitin-anand's answer was the most appropriate and cleaner than the rest, but that method is now deprecated and now we will have to create a react-native.config.js file in our root with the following configuration as an example:

module.exports = {
 project: {
  ios: {},
  android: {},
 },
 assets: ['./assets/fonts'], 
}; 
Muhammad Ovi
  • 1,053
  • 14
  • 26
1
  1. Set in Project.json:

    rnpm { 
        assets:assets/fonts
    }
    
  2. react-native link

valerybodak
  • 4,195
  • 2
  • 42
  • 53
1

For ios:

Add your fonts in given folder structure :

/assets/fonts

and place your fonts in it .

In the root folder . Add a file named

react-native.config.js

copy the code and paste

module.exports = {
assets: [‘./assets/fonts’]

}

1

you can easily add Google & custom fonts to react native projects via Expo-font.

1-Using google fonts in react native:

import expo-fonts:

 import { useFonts, Inter_900Black } from '@expo-google-fonts/inter';
        // install pakages related to your favourite font for example:@expo-google-fonts/roboto & etc.

then use this hook at the top of your component hierarchy:

    let [fontsLoaded] = useFonts({
        Inter_900Black,
      });
//fontLoaded indicates the loading state of your font

using font:

<Text style={{ fontFamily: 'Inter_900Black'}}>Inter Black</Text>

2-Using custom fonts in react native:

import expo-fonts:

import { useFonts } from 'expo-font';

use this hook at the top of your component hierarchy:

    let [fontsLoaded] = useFonts({
    'Custom-Font': require('./assets/fonts/Custom-Font.otf'),
  });

using font:

<Text style={{ fontFamily: 'Custom-Font'}}>Inter Black</Text>
Moein Moeinnia
  • 1,945
  • 3
  • 10
  • 26
0

Add in project.json file

rnpm {
assets:assets/fonts 
}

Then perform react-native link

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
0

The best way to do it would be to create your own custom component for Text and import it from another file.

Assuming you want to change the default font to "opensans-semibold" (that is the name I gave it after downloading it and saving it).

TYPESCRIPT:

import React from 'react';
import { Text as DefaultText, StyleSheet } from 'react-native';

export function Text(props : any) {
    return(
        <DefaultText style={[styles.defaultStyles, props.style]}> {props.children} </DefaultText>
    )
}
    
const styles = StyleSheet.create({
    defaultStyles: {
        fontFamily: "opensans-semibold"
    }
});

Now import this anywhere else as:

import { Text } from './path/to/component'

and use it as you normally would.

Karan Singh
  • 1,114
  • 1
  • 13
  • 30
0

The correct way

import React from 'react';
import { Text, View } from 'react-native';
import AppLoading from 'expo-app-loading';
import { useFonts } from 'expo-font';

export default props => {
  let [fontsLoaded] = useFonts({
    'Inter-Black': require('./assets/fonts/Inter-Black.otf'),
  });

  if (!fontsLoaded) {
    return <AppLoading />;
  }

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text style={{ fontFamily: 'Inter-Black', fontSize: 40 }}>Inter Black</Text>
      <Text style={{ fontSize: 40 }}>Platform Default</Text>
    </View>
  );
};
Abhinav
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 08 '22 at 06:35
0

For react-native version above 0.60, create a react-native.config.js file in the root of the directory and add the below code,

 module.exports = {
  assets: ['./assets/fonts'],
};

And you should also have the assets folder in root of the directory. Then just run the command npx react-native-asset in your terminal. This is should just work fine.

GerAlt
  • 13
  • 5
-1

becareful if assets in src folder

in react-native.config.js file

module.exports = {
project: {
    ios:{},
    android:{}
},
assets: ['./src/assets/fonts']// assets in src folder
// assets: ['./assets/fonts']// if assets in root use this

}

مصطفى
  • 555
  • 4
  • 9
-2

For Android :

put your custom fonts in the following folder:

Project folder/android/app/src/main/assets/fonts/font_name.ttf

Run react-native run-android

Use the font i your code:

title: { fontSize: 20, fontFamily: " font Name" },

  • That's basically a copy of already accepted answer. It does not bring anything new to the question. – Magiczne Sep 02 '20 at 07:41