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,

import { useFonts } from 'expo-font';

...After all the code in js file, and just before return, add the following code.

    const [loaded] = useFonts({
        Playfulist: require("./assets/fonts/Playfulist.otf"),
    });

    if (!loaded) {
        return null;
    }

    return(...rest of your app.js code here...)

Needless to say, ensure the file spelling matches correctly. 
Now, you would be able to use your font like you normally do using styling with fontFamily: 'Playfulist'

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.

import styles from "../src/styles";
import { Text } from "react-native";
const FontText = (props) => {
  return (
    <Text style={styles.alltext} {...props}>
      {props.children}
    </Text>
  );
};
export default FontText;

In your styles.js file, include the following styles:

alltext: {
    fontFamily: "Playfulist",
  },

Now, when rendering, (say in your app.js file), instead of using the <Text> tag, use the <FontText> tag.

import FontText from "./Utilities/FontText";

return (
    <View>
      <FontText>TEXT WITH CUSTOM FONT</FontText>
      <Text style={styles.alltext}>TEXT WITH CUSTOM FONT STYLING</Text>
      <Text>TEXT WITHOUT CUSTOM FONT</Text>
    </View>
)

Hope this helps!!


Comments

Popular posts from this blog

Reading a pre-populated sqlite database using react native with expo..

React Hooks - useState Vs useRef (An explanation)