04-Apr-2023
.
Admin
Hi Guys,
In this post, we will learn how to add custom fonts in react native. if you want to see example of how to create custom fonts in react native then you are a right place. we will help you to give example of react native custom fonts example. we will help you to give example of how to use custom fonts in react native. Here, Creating a basic example of how to implement custom fonts in react native.
Let's start following example:
Step 1: Download Project
In the first step run the following command to create a project.
expo init ExampleApp
Step 2: Install and Setup
In this step, you can install expo-font:
expo install expo-font
Step 3: App.js
In this step, You will open the App.js file and put the code.
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import * as Font from 'expo-font';
const App = () => {
const [fontsLoaded, setFontsLoaded] = useState(false);
const loadFonts = async () => {
await Font.loadAsync({
Gantari: require('./assets/fonts/Gantari-VariableFont_wght.ttf'),
'Lobster-Regular': {
uri: require('./assets/fonts/Lobster-Regular.ttf'),
display: Font.FontDisplay.FALLBACK,
},
});
setFontsLoaded(true);
}
useEffect(() => {
loadFonts();
}, []);
return (
<View style={styles.container}>
{fontsLoaded ? (
<View style={styles.container}>
<Text style={{ fontSize: 30 }}>Default Font</Text>
<Text style={{ fontFamily: 'Lobster-Regular', fontSize: 30 }}>Lobster-Regular</Text>
<Text style={{ fontFamily: 'Gantari', fontSize: 30 }}>Gantari</Text>
</View>
):
null
}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default App;
Run Project
In the last step run your project using the below command.
expo start
You can QR code scan in Expo Go Application on mobile.
Output :
It will help you...
#React Native