Using custom Fonts and applying it globally across the application
More recently, I had the need to implement a custom font for my app, and also apply it globally to the entire app (Including fontFamily in every <Text> tag seemed such a chore. After overdosing on google, and stackoverflow(our common chum! for all things code), I found the cleanest way to do it. So, here goes:
Step 1. Include the custom Font in your app.
Download the custom font. Note that it should have either the .ttf (TrueType font) or .otf (OpenType Font) extension. Both are supported by React Native.
What is the difference?
the OpenType font format (OTF) is the preferred choice for most professional designers because of its advanced typographic features and smaller file sizes. OTF files offer a much wider range of possibilities regarding type design. Plus, they provide better readability across multiple devices and platforms. They’re more modern-looking and offer a higher level of detail than TTF fonts.However, for average users, who do not need additional features like ligatures, extra characters, or alternative characters, a TrueType font is perfectly fine. It will have the same basic characters. It just won’t have the advanced functionality of the OTF version.
Find more information here
1. Download the font zip file. Unzip it and extract the required ttf files to {your project}/src/assets/fonts. This is a suggested folder. You can save it wherever you like in the path of your project folder.
2. install expo-font, expo-app-loading and react-native-asset
3. run the following command from your terminal:
npx react-native-asset
This will link your font files to your project.
4. Next, in your app.js file,
Step 2. Arrange it so it will apply globally to all <Text> tags.
It is a pain to include the custom fontFamily to every Text element. To be able to apply the font globally to every Text element, do the following:
1. Create a Utilities folder in the project folder, and create a file called FontText.js in the folder. You can place this anywhere you like, just ensure you import it correctly.
Comments
Post a Comment