A demo for Android font typeface support in React Native!
A demo for Android font typeface support in React Native!
Be able to use font typeface modifiers such as fontWeight and fontStyle in combination with a custom font family, in both iOS and Android.
Hello world!
Hello world!
For this example, we are going to register the Raleway font family. Of course, this method will work with any TTF font.
You need the whole Raleway font family, extracted in a temporary folder. That is:
Raleway-Thin.ttf (100)Raleway-ThinItalic.ttfRaleway-ExtraLight.ttf (200)Raleway-ExtraLightItalic.ttfRaleway-Light.ttf (300)Raleway-LightItalic.ttfRaleway-Regular.ttf (400)Raleway-Italic.ttfRaleway-Medium.ttf (500)Raleway-MediumItalic.ttfRaleway-SemiBold.ttf (600)Raleway-SemiBoldItalic.ttfRaleway-Bold.ttf (700)Raleway-BoldItalic.ttfRaleway-ExtraBold.ttf (800)Raleway-ExtraBoldItalic.ttfRaleway-Black.ttf (900)Raleway-BlackItalic.ttfWe will assume those files are now stored in /tmp/raleway/.
You will need otfinfo installed in your system to perform this step. It is shipped with many Linux distributions. On MacOS, install it via lcdf-typetools brew package.
otfinfo --family Raleway-Regular.ttfShould print "Raleway". This value must be retained for the Android setup.
This name will be used in React fontFamily style.
react-native init FontDemo
cd FontDemoFor Android, we are going to use XML Fonts to define variants of a base font family.
Remark: This procedure is available in React Native since commit fd6386a07eb75a8ec16b1384a3e5827dea520b64 (7 May 2019 ), with the addition of
ReactFontManager::addCustomFontmethod.
mkdir android/app/src/main/res/font
cp /tmp/raleway/*.ttf android/app/src/main/res/fontWe must rename the font files following these rules to comply with Android asset names restrictions:
- with _;You can use the below bash script (make sure you give the font folder as first argument):
#!/bin/bash
# fixfonts.sh
typeset folder="$1"
if [[ -d "$folder" && ! -z "$folder" ]]; then
pushd "$folder";
for file in *.ttf; do
typeset normalized="${file//-/_}";
normalized="${normalized,,}";
mv "$file" "$normalized"
done
popd
fi./fixfonts.sh /path/to/root/FontDemo/android/app/src/main/res/fontCreate the android/app/src/main/res/font/raleway.xml file with the below content. Basically,
we must create one entry per fontStyle / fontWeight combination we wish to support, and
register the corresponding asset name.
In android/app/src/main/java/com/fontdemo/MainApplication.java, bind the font family name with the asset we just created inside onCreate method.
:warning: If you are registering a different font, make sure you replace "Raleway" with the name found in the former step (find font family name).
…On React Native 0.73+
…On iOS, things will get much easier. We will basically just need to use React Native asset link functionality.
This method requires that we use the font family name retrieved in the first step
as fontFamily style attribute.
mkdir -p assets/fonts
cp /tmp/raleway/*.ttf assets/fontsreact-native.config.jsmodule.exports = {
project: {
ios: {},
android: {},
},
iosAssets: ['./assets/fonts'],
};# react-native >= 0.69
npx react-native-asset
# otherwise
react-native linkiOS Android
If you found this guide relevant, I would greatly appreciate that you upvote this StackOverflow answer. It would also help the community finding out this solution. Cheers!
No open issues yet, or sync has not completed.