facebook / react-native

A framework for building native applications using React

Home Page:https://reactnative.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding custom fonts in react native 60+

baterdene-dev opened this issue · comments

After react native 60 adding custom fonts have changed. Follow this short tutorial to simply add custom fonts in your app.
React Native CLI can be configured by creating a react-native.config.js at the root of the project. Depending on the type of a package, the set of valid properties is different. To add custom fonts we only need to add one line of code: assets: [‘./assets/fonts/’]
1_ezOw7B_Q4C5fUsAA9aKXGQ

Then add your fonts in ./assets/fonts/ directory

1_9QW7IHM1BEQckxUNL5r1oA

And use like this: fontFamily: ‘Roboto-Regular’
Just that simple we have custom fonts in our react native app :)
If you like the article, don’t forget to clap and share it :)

Via Sergo Kupreishvili

Thanks for submitting your issue. Can you take another look at your description and make sure the issue template has been filled in its entirety?

👉 Click here if you want to take another look at the Bug Report issue template.

For future visitors: run npx react-native link afterwards.

I've follow these steps, but after running npx react-native link I'm not getting any output either way.

  1. make a folder in root app as : assets then fonts fonts folder in assets and put ttf fonts
  2. make a file in root : react-native-config.js and add:
    module.exports = {
    project: {
    android: {}
    },
    assets: ['./assets/fonts/'],
    };
  3. react-native link
  4. style={{ fontFamily: "name of font" }}

I'm using React Native 0.60.5 and ran react-native link, not npx react-native link. Not sure what the difference is.

Probably should add that while this worked with Android right away, I had to go into XCode and build/run the app from there before my custom font could work in ios.

Hey @sudomann, the difference is that you installed react-native in your NodeJS global environment and you can run react-native link. The npx react-native link command can be ran when you installed react-native in your project dependencies and it'll find it into you node_modules/ project directory. 😄

why the font name is named another name ? how to query?

@shenl923 I'm not sure I understand what you are asking. Explain better?

@shenl923 are you referring to the fact that Android uses the font's filename whereas iOS uses the font's PostScript name? In case yes, this article here describes this issue, maybe it helps.

react-native link doesn't work for v. 0.60

@jallynme
Create a file in your react native project called react-native.config.js:
In it, paste

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

assets: ['./src/assets/fonts/'], should be the path to wherever you stored your custom fonts in your project folder.
When you run the link command, it should work then.

Seems like this here is a duplicate of #25697?
This here should be the solution to our problems with linking and rn >=0.60: https://www.npmjs.com/package/react-native-asset

commented

Facing this issue on Android!
any solution ?

Using:

    "react": "16.9.0",
    "react-native": "0.61.2",

where do I have to create the file react-native.config.js?
where is the root of a project?

where do I have to create the file react-native.config.js?
where is the root of a project?

When you create a react native project, you give it a name, and a folder is created with that name. That is the root. In that project root folder, there should be at least 2 folders, android/ and ios/.

For example I start a new react native project with $ react-native init myProjectName
Then cd myProjectName, I am now in the root of my project

For future visitors: run npx react-native link afterwards.

And, if you have more than one font and you have one folder for each font you have to add one by one something like this

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

@MiguelGrability You shouldn't have to list out each font, for our project we are doing:

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

@saadq If you add new font files to './assets/fonts/', does running the link command again update the native assets with the new fonts, or can this be achieved only by typing in new folders to the assets array? (like @MiguelGrability did).

I haven't tried adding new fonts after my initial setup, but I believe you would need to run npx react-native link after you update the config file.

Doesn't work for me on react-native 0.60.5, adding this to package.json however did

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

@BaterdeneA @react-native-bot
Hi, I applied these steps in my project but not worked

"react": "16.9.0",
  "react-native": "0.61.4",

I added react-native.config.js

module.exports = {
  project: {
    ios: {},
    android: {}, // grouped into "project"
  },
  assets: ['./assets/fonts/'], // stays the same
};

Even i run "react-native link" command after check all things but nothing works yet.
Anyone please help ?

If you're using expo, you might want to check this guide.
https://docs.expo.io/versions/latest/guides/using-custom-fonts/

commented

"react-native": "0.60.5"

Font for android dont work

@BaterdeneA @react-native-bot
Hi, I applied these steps in my project but not worked

"react": "16.9.0",
  "react-native": "0.61.4",

I added react-native.config.js

module.exports = {
  project: {
    ios: {},
    android: {}, // grouped into "project"
  },
  assets: ['./assets/fonts/'], // stays the same
};

Even i run "react-native link" command after check all things but nothing works yet.
Anyone please help ?

same :(

@BaterdeneA @react-native-bot
Hi, I applied these steps in my project but not worked

"react": "16.9.0",
  "react-native": "0.61.4",

I added react-native.config.js

module.exports = {
  project: {
    ios: {},
    android: {}, // grouped into "project"
  },
  assets: ['./assets/fonts/'], // stays the same
};

Even i run "react-native link" command after check all things but nothing works yet.
Anyone please help ?

same :(

Same here :(
Using:

"react": "16.9.0",
"react-native": "0.61.5"
commented

Same here :(
Using:

"react": "16.9.0",
"react-native": "0.61.5"

on android

npx react-native link runs fine
info Linking assets to ios project
info Linking assets to android project
success Assets have been successfully linked to your project

and the generated app-debug.apk does contain the ttf files under assets\fonts
however when using
fontFamily: "Dancing Script"
the font is now shown

commented

finally get it working
the font-name in the
fontFamily: "opensanscondensed"
need to be exact match for the ttf file
opensanscondensed.ttf

For future visitors: run npx react-native link afterwards.

Also, make sure that the font name is correct. On macOS, you can right click on the .ttf file and hit Get Info to find the full name of the font. Use that as a parameter to fontFamily.

commented

i have a solution for Android

go to

android/app/src/main/assets/Fonts.xml

use the same name of the fonts, but android separate de fonts my type: regular, italic and bold

if you style contain propertie "bold" and your font no have in this archive (Fonts.xml), this dont work (you can edit your Fonts.xml to solve the problems)

protip:

Font_Title: { ...Platform.select({ ios: { fontFamily: NameFont_iOs', }, android: { fontFamily: NameFont_Android', }, }), },

DO THIS

After react native 60 adding custom fonts have changed. Follow this short tutorial to simply add custom fonts in your app.
React Native CLI can be configured by creating a react-native.config.js at the root of the project. Depending on the type of a package, the set of valid properties is different. To add custom fonts we only need to add one line of code: assets: [‘./assets/fonts/’]
1_ezOw7B_Q4C5fUsAA9aKXGQ

Then add your fonts in ./assets/fonts/ directory

1_9QW7IHM1BEQckxUNL5r1oA

And use like this: fontFamily: ‘Roboto-Regular’
Just that simple we have custom fonts in our react native app :)
If you like the article, don’t forget to clap and share it :)

Via Sergo Kupreishvili

AND THIS

For future visitors: run npx react-native link afterwards.

THEN THIS
Re-run your app to initialize with the newly bundled resources (i.e the fonts)
npx react-native run-android

Should work fine!!

EXTRA HELP
See https://medium.com/@mehran.khan/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4

I saw this in stackoverflow and worked for me

"I just renamed my file react-native-config.js to react-native.config.js and it worked."

Had this same error for about 2-3 hours...

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

npx react-native link did not work.

But after moving the fonts folder into a new folder assets in the root level and editing the config to

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

the npx react-native link command passed. Not sure why the assets folder needs to be in the root level of the project for it to link up.

Hi!

I use:

"react": "16.9.0",
   "react-native": "0.61.5"

this worked for me:

  1. in react-native.config.js:
module.exports = {
  project: {
    android: {},
  },
  assets: ["./android/assets/fonts/"],
};
  1. run:

react-native link

Or maybe I just need better glasses

Had this same error for about 2-3 hours...

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

npx react-native link did not work.

But after moving the fonts folder into a new folder assets in the root level and editing the config to

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

the npx react-native link command passed. Not sure why the assets folder needs to be in the root level of the project for it to link up.

Or maybe I need too pay a little more attention to the paths.. Should have been ./src instead of .src..

Or maybe I just need better glasses

Had this same error for about 2-3 hours...

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

npx react-native link did not work.
But after moving the fonts folder into a new folder assets in the root level and editing the config to

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

the npx react-native link command passed. Not sure why the assets folder needs to be in the root level of the project for it to link up.

Or maybe I need too pay a little more attention to the paths.. Should have been ./src instead of .src..

nice

where is this mentioned in the documentation? what are the other usages of react-native.config.js?

Closing after several months without followup from the author. Please make sure to fill in the issue template if you decide to file a new issue.

Closing after several months without followup from the author. Please make sure to fill in the issue template if you decide to file a new issue.

I think everyone is waiting for a fix.

Renaming the fonts and removing the "-" worked for me. Linking correctly.

So... how do we use custom fonts. A lot of people, myself included are having problems.

I've checked the filename, the font name, the assets being copied properly, the react-native.config.js file, the works.

Are there some rules we need to know about the filenames/font names?


Of course 4 minutes after I post this I find the problem.

Here's the latest guide for React Native 0.61.5 custom font anyone stuck on this:

Create at root a react-native.config.js file:

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

Create a folder at root assets/fonts and past in your font, the file name doesn't matter at all here. Make sure it's a .ttf file.

Finally, in your operating system find the font name, in Mac I had to install the font, and use it's PostScript name for it to work.

image

All you need to do then is run yarn react-native link.

Then yarn ios to run your app in your ios sim.

This worked for me:

Create a folder at root assets/fonts and past custom-font.ttf file in there.

in react-native.config.js:

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

run:
react-native link

Re-run your app to initialize with the newly bundled resources (i.e the fonts)

npx react-native run-android

Hey @rachelnabors @hramos this(fonts) seems a bit undocumented in the React Native docs, and I think is a common use case in React Native apps (happening to me atm). Should we try to improve it? 😺

@sergiotapia-papa Hey How can i handle this with react-native-vector-icon?

Ok, I've been struggeling with this today and luckily I found my issue.

When I did "react-native link" the font files were copied to:

android/src/main/assets/fonts

This is the wrong location, the location that it should have been was:

android/app/src/main/assets/fonts

When I copied them manually and ran "react-native run-android" again the fonts finally worked!

@timvandijck You are correct. This is happening for me on 0.62. I was following this guide.

I saw this in stackoverflow and worked for me

"I just renamed my file react-native-config.js to react-native.config.js and it worked."

Its work for me ❤️

I saw this in stackoverflow and worked for me

"I just renamed my file react-native-config.js to react-native.config.js and it worked."

It's worked for me.

react-native link doesn't work for v. 0.60

yarn link

Still have the issue with loading custom font into react native project
when I run react-native link it will add many fonts besides my font into the info.plist and android fonts directory. and also running yarn ios fails after this.

Is there any official documentation that explains how we can achieve loading custom fonts without such messy activities?

@jallynme
Create a file in your react native project called react-native.config.js:
In it, paste

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

assets: ['./src/assets/fonts/'], should be the path to wherever you stored your custom fonts in your project folder.
When you run the link command, it should work then.

Thank! It work for me.
"react": "16.11.0",
"react-native": "0.62.2",

  1. make a folder in root app as : assets then fonts fonts folder in assets and put ttf fonts
  2. make a file in root : react-native-config.js and add:
    module.exports = {
    project: {
    android: {}
    },
    assets: ['./assets/fonts/'],
    };
  3. react-native link
  4. style={{ fontFamily: "name of font" }}

Hi,
the filename should be react-native.config.js instead of react-native-config.js . There's a typo in your instruction.

Thanks.

Remember to run yarn android or yarn ios after run npx react-native link. I tried it and it works well

The solutions mentioned in this post did not work for me, this worked in react-native.config.js
module.exports = { "assets": [ "fonts" ],

and commented out the assets line in babel.config.js
module.exports = { presets: ['module:metro-react-native-babel-preset'], // assets: ['fonts'], };

also added a assets > fonts > individual font files folder in my root

Will there be an official Article How to add custom fonts in the react-native documentation soon? (Or does I only missed it?)
I think it's an important part, which should not be missed in the documentations.

I've been having a similar issue. I'm trying to set up a custom font on a project, but running npx react-native link doesn't seem to work.

My React Native Info

System:
    OS: macOS Mojave 10.14.5
    CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
    Memory: 301.42 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 13.6.0 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 13.1, DriverKit 19.0, macOS 10.15, tvOS 13.0, watchOS 6.0
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3, 29.0.2
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5900203
    Xcode: 11.1/11A1027 - /usr/bin/xcodebuild
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.5 => 0.61.5

My react-native.config.js (I double checked that this is not mispelled):

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

I added a font at assets/fonts/myfont.ttf. I ran npx react-native link and I got:

info Linking assets to ios project
warn Group 'Resources' does not exist in your Xcode project. We have created it automatically for you.
info Linking assets to android project
success Assets have been successfully linked to your project

This is the diff it made on my Info.plist, it created the UIAppFonts key, but it didn't add the font.

        <key>UIViewControllerBasedStatusBarAppearance</key>
        <false/>
+       <key>UIAppFonts</key>
+       <array/>

This is what it changed in my project. I was expecting it to add the font in the android directory, but it didn't do anything to android/

➜  conversations-rn-ui git:(raf-ui-icon-20-05-19) ✗ git status
On branch raf-ui-icon-20-05-19
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   ios/ConversationsMobile.xcodeproj/project.pbxproj
	modified:   ios/ConversationsMobile/Info.plist

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	assets/
	react-native.config.js

The information proposed in this post does not work for me. Are there other solutions?
react-native-cli: 2.0.1
react-native: 0.61.4

I found and tried these solutions but they don't work for me https://ospfolio.com/two-way-to-change-default-font-family-in-react-native/

The information proposed in this post does not work for me. Are there other solutions?
react-native-cli: 2.0.1 react-native: 0.61.4

I found and tried these solutions but they don't work for me https://ospfolio.com/two-way-to-change-default-font-family-in-react-native/

Did you check out my solution from 5 days back?

Did you check out my solution from 5 days back?

Yes I did, but it didn't work!

I migrated to 0.62.2 at the weekend, and am now unable to get the custom fonts working. I just get the Unrecognized font family message.

I've followed the this post: https://medium.com/@mehran.khan/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4 and it didn't work for me on iOS.

I'm trying to add the Roboto font from Google.

version

"react": "16.11.0",
"react-native": "0.62.2",

I created the <root>/assets/fonts.
Screen Shot 2020-05-20 at 11 59 42

Configured the react-native.config.js as explained

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

After that I ran the npx react-native link
Screen Shot 2020-05-20 at 11 58 25

I've checked the Info.plist.
Screen Shot 2020-05-20 at 12 01 37

Checked the TARGET -> Copy Bundle Resources too.
Screen Shot 2020-05-20 at 12 03 33

Added the Roboto fontFamily.

const styles = StyleSheet.create({
  custom: {
    fontFamily: 'Roboto-Regular',
    fontSize: 32
  }
});

And I'm getting the same error.

Screen Shot 2020-05-20 at 12 06 44

I've followed the this post: https://medium.com/@mehran.khan/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4 and it didn't work for me on iOS.

I don't know why, maybe because it's a post from Jan 12, 2019... many things, releases and upgrades have changed since that day. I've also tried several solutions but I've not yet managed to load custom fonts.

@jmlavoier Are you sure you restarted the packager and rebuild the app?

@tusharkhatiwada Yes, I restarted the server and rebuilt the app.

https://callstack.github.io/react-native-paper/fonts.html this link and my comment from #25852 (comment) a few days back worked for me. Take care about the react native version and the related install instructions

I'ts worked for me:

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

commented

I have different variants (schemes) and I do not want some fonts to be added in some variants (or some fonts to be added only to a particular variant, etc.).

I assume that this cannot be done through the "config" file? I need to do this manually?

It wasn't working for me because I set a bold property in the CSS, when I removed that property the font was displayed. So I used the bold font itself directly.

It wasn't working for me because I set a bold property in the CSS, when I removed that property the font was displayed. So I used the bold font itself directly.

It's not my case.

same as @JonatanOrtiz figured out, I've found that at least, it only works for me, if I set the fontWeight in the stylesheet to normal.
Seems you have to use an Font-File which was specially made for bold instead of setting it via Stylesheet to other than to fontWeight: 'normal',.

Example for Font-Face "Quicksand":

titleStyle: {
  fontFamily: 'Quicksand-Bold'
},
contentText: {
  fontFamily: 'Quicksand-Regular'
},

On google-webfonts, there are most of the time different files for differeent font-weights inside the Package.

It wasn't working for me because I set a bold property in the CSS, when I removed that property the font was displayed. So I used the bold font itself directly.

It's not my case.

"Note: One thing to be cautious of when integrating custom fonts into your project is that Android will use the font file name when referencing the font, whereas IOS will use the postscript name. To check the postscript name of the file you will need to use Font Book. So in my example, I will be checking the postscript name of the font Montserrat.
As you can see from the screenshot below, the full name for the Montserrat font is Montserrat Regular; however, the postscript name is Montserrat-Regular, so, if necessary, you’ll need to rename the font files to be the postscript file name. However, with Montserrat the font file name is already like the postscript name, so for this font I don’t have to do it."
https://medium.com/better-programming/how-to-add-custom-fonts-to-your-react-native-project-c64305281b9

Maybe that will help you.

Just FYI haven't read everything but make sure to clean your builds before expecting the font to be included in the app.

For me the react-native.config.js + npx react-native link + cleaning my build folder worked flawlessly.

XCode => CMD + Shift + K
or => Product => Clean Build Folder

Not sure about android as of now, maybe just remove the build folder manually
rm -rf ./android/build?

@jmlavoier I have the same issue. did you find a solution?

@jmlavoier I have the same issue. did you find a solution?

I had the same issue despite the clear instructions. For some reason, it didn't work over iOS, so I tried to add a reference link directly in Xcode in:

"Build Phases" -> "Copy Bundle Resources" -> "+" -> "#select your font from assets/fonts folder"

I also added it in my iOS Info.plist:

<key>UIAppFonts</key>
<array>
<string>Your-Font-File-Name.ttf</string>
</array>

and it worked.

  1. make a folder in root app as : assets then fonts fonts folder in assets and put ttf fonts
  2. make a file in root : react-native-config.js and add:
    module.exports = {
    project: {
    android: {}
    },
    assets: ['./assets/fonts/'],
    };
  3. react-native link
  4. style={{ fontFamily: "name of font" }}

Hi,
the filename should be react-native.config.js instead of react-native-config.js . There's a typo in your instruction.

Thanks.

Life saver !!!

If you don't see any changes after taking the above steps, try exiting the app and run npx react-native run-android

@Michaelvons Hey, I'm trying your way but it does not work for iOS,
I got this error


Build system information
error: Multiple commands produce '/Users/anas/Library/Developer/Xcode/DerivedData/ourprophet-dgyemahaekpmtsewexlythyooogi/Build/Products/Debug-iphonesimulator/ourprophet.app/AntDesign.ttf':
1) Target 'ourprophet' (project 'ourprophet') has copy command from '/Volumes/Work/Works/client-3 Qiam App /qiamApp/QiamApp/node_modules/native-base/Fonts/AntDesign.ttf' to '/Users/anas/Library/Developer/Xcode/DerivedData/ourprophet-dgyemahaekpmtsewexlythyooogi/Build/Products/Debug-iphonesimulator/ourprophet.app/AntDesign.ttf'
2) That command depends on command in Target 'ourprophet' (project 'ourprophet'): script phase “[CP] Copy Pods Resources”

  1. make a folder in root app as : assets then fonts fonts folder in assets and put ttf fonts
  2. make a file in root : react-native-config.js and add:

root folder name should be react-native.config.js

@Michaelvons Hey, I'm trying your way but it does not work for iOS,
I got this error


Build system information
error: Multiple commands produce '/Users/anas/Library/Developer/Xcode/DerivedData/ourprophet-dgyemahaekpmtsewexlythyooogi/Build/Products/Debug-iphonesimulator/ourprophet.app/AntDesign.ttf':
1) Target 'ourprophet' (project 'ourprophet') has copy command from '/Volumes/Work/Works/client-3 Qiam App /qiamApp/QiamApp/node_modules/native-base/Fonts/AntDesign.ttf' to '/Users/anas/Library/Developer/Xcode/DerivedData/ourprophet-dgyemahaekpmtsewexlythyooogi/Build/Products/Debug-iphonesimulator/ourprophet.app/AntDesign.ttf'
2) That command depends on command in Target 'ourprophet' (project 'ourprophet'): script phase “[CP] Copy Pods Resources”

Since you are on ios, the right command would be npx react-native run-ios
Also, follow the steps by @tusharkhatiwada before taking mine.

https://medium.com/@mehrankhandev/ultimate-guide-to-use-custom-fonts-in-react-native-77fcdf859cf4 worked on iOS and Android with RN 0.62.2. If it's not working for you:

Double-check

  1. Are your fonts in TTF format?

  2. Did you rename font files as in the article (filename without prefix matches PostScript name)?

  3. Have a) font files been copied to /android/app/src/main/assets/fonts and b) do ios/MyProjectName/Info.plist & ios/MyProjectName.xcodeproj/project.pbxproj contain rows with the font file names? If not:

    • Did you run npx react-native link (or react-native link, depending on your setup)?
    • Did you create the react-native.config.js as in the article? (note the two dots, .config.js)
  4. Do you use fontWeight? If yes, try removing it (can cause problems on Android, see https://stackoverflow.com/a/58765980 ).

  5. Is your style definition correct, e.g. style={{ fontFamily: "SFUIDisplay-Bold" }}? Try it on other <Text> elements in your app to weed out problems related to the element itself (e.g. the fontWeight issue).

  6. Have you stopped the Metro bundler and any emulators (e.g. rebooted your machine)?

  7. Have you tried other fonts than the ones you actually want? Try e.g. the fonts in the article to rule out problems with your fonts.

  8. Are you using Expo instead of vanilla React Native? If yes, check out https://docs.expo.io/guides/using-custom-fonts/

Good luck!

commented

if you have multiple targets on iOS (e.g a staging build) react-native link will only add the fonts to the main target, you can update the others in build-phases -> copy bundle resources and then update your info.plist to include these fonts as well.

commented

It still doesn't work for me on android "react-native": "0.62.2".
My path to fonts:

  • android/app/src/main/assets/fonts
  • src/assets/fonts

This is what I tried:

  1. Creating react-native.config.js file on root project with the following code:
    project: {
        ios: {},
        android: {}, // grouped into "project"
    },
    assets: ['./src/assets/fonts']
}
  1. Running react-native link: This didnt give me any errors:
info Linking assets to android project
success Assets have been successfully linked to your project
  1. I use react-native-global-font
        let fontName = 'Montserrat-Regular' //'Museo500-Regular'
        GlobalFont.applyGlobal(fontName)
     } ```

4. And try naming the font exactly as its named in styles WITHOUT using any fontWeight.


Is, there a way to link fonts only?
I don't want to link other dependencies which already has autolinking enabled by doing npx react-native link.

I have followed this tutorial and it's working fine for android and ios both. The naming of the font should exactly the same as it's named in styles WITHOUT using any fontWeight.
For i.e.

  textLargeStyle: {
    fontSize: 40,
    textAlign: 'center',
    fontFamily: 'Bradley-Hand-Bold', //<<<<--- Here is the Trick
  },
  textSmallStyle: {
    fontSize: 35,
    marginTop: 20,
    marginBottom: 30,
    textAlign: 'center',
    fontFamily: 'DancingScript-Regular', //<<<<--- Here is the Trick
  }

I got one issue in ios(Unrecognized font-family) and the issue was font name I am using in style is different from actual font name(PostScript Name - That can be get by open it with any font editor and then view details). I have used birdfont.
So

import {Platform} from 'react-native';

  textLargeStyle: {
    fontFamily: Platform.OS === 'ios' ? 'BradleyHandITCTT-Bold' : 'Bradley-Hand-Bold';
  }

@jmlavoier I have the same issue. did you find a solution?

I've stopped the iOS version, I'm just following with Android for a while. Whether I find the solution I will try again.

Solution for iOS :
Run npx react-native link this will create resources folder in xcode. After finish open xcode and Right Click Resources and choose Add file to "<Project>", find your custom font press Add.

Solution for Android :
Create folder assets/fonts in path android/app/src/main then copy the custom font you want add.
npx react-native link will help you copy react-native-vector-icons fonts automatically.

Error on iOS
Tips error duplicate **“[CP] Copy Pods Resources” (iOS)**
remove vector icons fonts from folder Resources

Good Luck

Removing default style property like fontWeight also solves the problem because custom font has its own styling properties and will not work if you have added default react-native styling property like fontWeight.

@shiroze That's way work for Android,
In ios, I got this error :(
Unrecognized font-family 'dnfFont'

@shiroze That's way work for Android,
In ios, I got this error :(
Unrecognized font-family 'dnfFont'

Did you Add file to "<Project>" ?

After react native 60 adding custom fonts have changed. Follow this short tutorial to simply add custom fonts in your app.
React Native CLI can be configured by creating a react-native.config.js at the root of the project. Depending on the type of a package, the set of valid properties is different. To add custom fonts we only need to add one line of code: assets: [‘./assets/fonts/’]
1_ezOw7B_Q4C5fUsAA9aKXGQ

Then add your fonts in ./assets/fonts/ directory

1_9QW7IHM1BEQckxUNL5r1oA

And use like this: fontFamily: ‘Roboto-Regular’
Just that simple we have custom fonts in our react native app :)
If you like the article, don’t forget to clap and share it :)

Via Sergo Kupreishvili

Tried but no luck for RN V0.63.2.
Created a file react-native.config.js

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

info Linking assets to ios project info Linking assets to android project success Assets have been successfully linked to your project

image

react-native link doesn't work for v. 0.60

use npx react-native link

OS:
"react": "16.9.0", "react-native": "0.61.5",

I Resolved by:

  • Remove all FontWeight
  • Copy font to android/app/src/main/assets/fonts
  • using exact match for the ttf file

Example:
Screen Shot 2020-08-24 at 17 07 57

And my style
Before:
{ fontWeight: 'bold', fontFamily: 'Arimo-Bold', color: colors.black, fontSize: fonts.normal, }

After:
{ fontFamily: 'Arimo-Bold', color: colors.black, fontSize: fonts.normal, }

same issue. tried everything. same issue

@habeebtheprogrammer you try to stop all the app and run again? work for me

npx react-native link worked for me, but be careful, file name should be react-native.config.js not react-native-config.js

I've written complete step by step guide for linking fonts for both iOS and android.

Thanks sirajalam049 your solution worked for me.

still i have face same issue on 0.64.0

Solution

please copy paste your font file to

android/app/src/main/assets/fonts

if you have not find the folder of asset inside main folder. please create it assets/fonts folder

Then do
react-native link

its working now without adding any config