Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add fonts for different font weights for react-native android project?

Tags:

I've managed to add a custom font by:

  • putting *.ttf files in ProjectName/android/app/src/main/assets/fonts/ like this:

    • Open Sans.ttf
    • Open Sans_italic.ttf
    • Open Sans_bold.ttf
    • Open Sans_bold_italic.ttf
  • and setting font family by fontFamily: "Open Sans"

But there are extra font weights I want to use like 'Semi Bold', 'Extra Bold'. I tried adding them as 'Open Sans_900.ttf' and setting fontWeight: 900 but that didn't work, it displayed bold version of the font.

Is there a way to add these additional font weights?

like image 975
Gokhan Sari Avatar asked Aug 07 '16 14:08

Gokhan Sari


People also ask

How do I import Google fonts into React Native?

You can fetch your fonts from Google Fonts. In your React Native project src directory, you should create an assets directory. Here you'll have a directory for each kind of asset (SVG, Fonts, etc). Therefore, create a fonts directory and paste the fonts that you downloaded.

How do I change my font family in react?

To set a global font family in React, set the font-family style on the html element in your index. css file and import the file in your index. js file. Global CSS should be imported in index.

Can you upload fonts to PageFly?

Open your PageFly page and click on Add custom Code button Now you need to decide if you want to apply the custom font to the whole page or to the specific element.


1 Answers

The out of the box support for custom fonts on Android is a little limited in React Native. It does not support font weights other than normal and bold (it also supports the italic fontStyle). If you want other custom weights, you must add them as separate fonts with a different name (as David mentioned in his answer).

The only font files that RN will find are of the following format:

  • {fontFamilyName}
  • {fontFamilyName}_bold
  • {fontFamilyName}_italic
  • {fontFamilyName}_bold_italic

Supported extensions: .ttf and .otf

This really isn't documented anywhere (that I know of), but you can read the Font Manager code here: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/views/text/ReactFontManager.java

like image 94
Dan Horrigan Avatar answered Sep 21 '22 20:09

Dan Horrigan