36

I would like to use custom font in my app, but I keep getting error

[RCTLog][tid:0x78f70450][RCTConvert.m:581]>Unrecognized font family 'Roboto'

Does this mean I have to mess with the iOS's way to import font directly?

Gott Phusit
  • 373
  • 1
  • 3
  • 5

8 Answers8

39
  1. Create a new group Fonts within your Xcode project

  2. Drag and drop fonts from Finder to the Fonts group you just created, and check your project name in Add to targets list

  3. Expand your project name folder within the main directory in your project and open Info.plist

  4. Add Fonts provided by application as a new key

  5. Add a new item under Fonts provided by application, and set the item value to the full font name with extension for each font you've added to the fonts folder

    • i.e. if the font file name is OpenSans-ExtraBold.ttf, then that should be the value of the item.
  6. To use the font in React Native, simply set the fontFamily style attribute for the element (see this). Note that the style should use the name of the font rather than the file name.

    • e.g. in the example above, the style attribute would be fontFamily: 'Open Sans'
  7. Run Shift + Command + K to clean last build

  8. Then Command + B to start a new build

  9. And finally Command + R to run the application

If you still see the error Unrecognized font family restart your react packager.

https://github.com/alucic/react-native-cheat-sheet

Hope it helps!

tohster
  • 6,973
  • 5
  • 38
  • 55
alucic
  • 12,492
  • 2
  • 20
  • 25
  • 1
    OH the step 10. restart react packager did the work. Thanks :) – cjmling Sep 12 '16 at 09:09
  • 1
    Needed to add it to "Copy Bundle Resources" as well. – Brian F Nov 10 '16 at 04:26
  • I forgot to append '.ttf' the suffix... THX! – Sai Nov 29 '17 at 03:53
  • this also works: `right click Fonts and select "Add files to "` then select font files on finder – Thom Oct 05 '18 at 12:01
  • All answers are somewhat related to prepackaging the font in the project, anyone who has done dynamic downloading of the font in runtime and saving it and then using it. Typically Chinese fonts are huge and increase the app size drastically. – Voonic Dec 14 '20 at 10:16
32

UPDATE

You don't need to install rnpm manually now. After step one, in step two, you can just use the command react-native link and all your assets will be linked. rnpm is now being merged with react-naitve. Checkout this commit on RN https://github.com/facebook/react-native/commit/e8b508144fdcdea436cf4d80d99daec757505c70


There is an easier way of doing things through `rnpm. It adds fonts to both android and ios. Place the font you want to add to a suitable location inside your project directory. In your package.json just add the following:

step1

...
    "rnpm": {
          "assets": ["path/to/your/font/directory"]
    },
...

step2

Then from command line do: rnpm link

you can now happily use your fonts in your app.

Note: you have to have rnpm installed for this to work. Just do npm install -g rnpm

Here is the documentation: https://github.com/rnpm/rnpm#advanced-usage

Ariona Rian
  • 9,153
  • 3
  • 24
  • 36
Aakash Sigdel
  • 9,060
  • 5
  • 33
  • 38
  • now that rnpm is merged into react-native, I should be abe to do it from react-native directly, correct? – André Junges Aug 09 '16 at 12:13
  • I haven't tested it, but it should be possible. I will update the answer once I've tested it. @AndréJunges – Aakash Sigdel Aug 18 '16 at 01:13
  • It didn't work for me for IOS - I had to follow the steps from @alucic`s answer – André Junges Aug 18 '16 at 01:19
  • I just tried it out, it works for me. Did you rebuild the project after running react-native link? You will still need to add the `rnpm` entry to the package.json through. @AndréJunges – Aakash Sigdel Aug 18 '16 at 01:30
  • hmm, Rereading now I may have missed one step. I pointed the font path to the original directory, outside the project.. like `/Library/Fonts/..` – André Junges Aug 18 '16 at 01:48
  • Thanks @AakashSigdel. Worked and much easier than the other solutions. – pmont Nov 03 '16 at 20:45
  • 2
    One detail missing here is that the "fontFamily" in react native should be the name of the font, not the name of the font file (on Mac you can open up the font in Font Book to see the font name, which appears in the title bar) – iainbeeston Jan 25 '17 at 14:53
23

The answer from Aakash is correct.

Method:

  1. Add your font to a folder in your react-native project
  2. Add to package.json the following
 "rnpm": {
    "assets": ["./your/fonts/folder"]
  }
  1. run react-native link in your terminal

Optionally:

  1. add rnpm using npm install -g rnpm
  2. run rnpm link

Example usage

  1. I put myFont.ttf in a folder whose path (relative to package.json) is ./src/assets/fonts
  2. I put the following code in package.json file
  "rnpm": {
    "assets": ["./src/assets/fonts"]
  }
  1. I run in terminal

react-native link

  1. I use it in a Text element in react-native like this: (without the space after the first < and without the space before the last > )

< Text style={{ fontFamily: myFont }}> Hello MyFont! < /Text >

Images: package.json link

Thanks for the great answer Aakash, hope you don't mind the re-breakdown for noobs like myself out there!

AWrightIV
  • 553
  • 7
  • 15
Ally Haire
  • 2,398
  • 1
  • 14
  • 19
  • When I run `react-native link` then font I added in my assets/fonts is being removed automatically, how to solve this because the font still not working – Website Is Fun Jan 17 '17 at 05:48
  • hi I'm not sure what is happening.. where are you placing the fonts exactly? (ie i use atom editor and put the folder in there). A snapshot of the issue may be useful? – Ally Haire Jan 20 '17 at 03:24
  • 1
    It is working now, I just added it under **/android/app/src/main/assets/fonts/** and then avoid using `fontWeight: 'bold'` on the style because it breaks the custom fonts making it not to work. Re-build the app and done. :) – Website Is Fun Jan 20 '17 at 06:23
  • should i import the font? – otto Mar 16 '18 at 21:12
  • should i import the font? – otto Mar 16 '18 at 21:12
21

That's because iOS does not have the Roboto font available by default.

Exactly. To make it available you have to include it in your project from Xcode. Just follow the steps that this answer provides, it's pretty straightforward.

Once you have done this, then you should have no problem using it from JS.

Community
  • 1
  • 1
mttcrsp
  • 1,651
  • 16
  • 14
5

Many answers are here for react-native 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 = {
  assets: ['./assets/fonts/']
};

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

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45
0

If you are using Create React Native App, when running react-native link you will see this:

react-native link can not be used in Create React Native App projects. If you need to include a library that relies on custom native code, you might have to eject first. See https://github.com/react-community/create-react-native-app/blob/master/EJECTING.md for more information.

If all you are trying to do is to add custom fonts, instead of ejecting, you could use Expo, which is also included by default with CRNA. Essentially, you do:

await Font.loadAsync({
  'open-sans-bold': require('./assets/fonts/OpenSans-Bold.ttf'),
});

Also, you can only do ttf or otf formatted fonts, no ttc fonts: https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app

Here is a detailed doc from Expo: https://docs.expo.io/versions/latest/guides/using-custom-fonts

randomor
  • 5,329
  • 4
  • 46
  • 68
0
  1. add the font files (i.e. .otf OR .ttf) in root/assets/font folder in your project.

  2. in package.json: rnpm: "assets": ["./assets/font"],

    1. if after building your fonts are not being supported then add this one file in root level:

react-native.config.js

 module.exports = {
    project: {
        ios: {},
        android: {},
    },
    assets: ['./assets/fonts']
    };
  1. ex: font-name -> Robotto-Regular.ttf then use it like below in styles:
fontFamily: 'Robotto-Regular'
  1. run react-native link
  2. Then in android side(android studio) and ios side(Xcode) sync the project.
  3. run react-native run-android OR react-native run-ios

This should resolve any errors which will be coming of fonts not supporting and you can use your custom fonts.

amit pandya
  • 1,384
  • 13
  • 22
0

create a file i.e defaultconfig.js

and in this file add the fonts then use

in widget by fontfamily:''montoface