I am a beginner learning React Native and I am trying to include custom fonts (.ttf files) in my application.
I get warnings like these:
fontFamily "DMRegular" is not a system font and has not been loaded through expo-font
This is what I have done in my _layout.js file:
import { Stack } from 'expo-router'
import { useCallback } from 'react'
import { useFonts } from 'expo-font'
import * as SplashScreen from 'expo-splash-screen'
SplashScreen.preventAutoHideAsync()
const Layout = () => {
const [fontsLoaded] = useFonts({
DMBold: require('../assets/fonts/DMSans-Bold.ttf'),
DMRegular: require('../assets/fonts/DMSans-Regular.ttf'),
DMMedium: require('../assets/fonts/DMSans-Medium.ttf'),
})
const onLayoutRootView = useCallback(async () => {
if(fontsLoaded) {
await SplashScreen.hideAsync();
}
}, [fontsLoaded])
if(!fontsLoaded) return null;
return <Stack onLayout={onLayoutRootView} />;
}
I am trying to use the custom fonts like this:
const styles = StyleSheet.create({
welcomeMessage: {
fontFamily: "DMBold",
fontSize: SIZES.xLarge,
color: COLORS.primary,
marginTop: 2,
}
Please, any help would be much appreciated.
Versions:
"expo": "~50.0.14"
"expo-font": "~11.10.3"
To include custom fonts in your React Native application using Expo, you need to follow these steps:
Import Expo Font: Make sure you import expo-font in your project.
Load Fonts: Use Font.loadAsync() to load your custom fonts before rendering any UI components that use them.
Use Custom Fonts: Once the fonts are loaded, you can use them in your styles by specifying the fontFamily.
Here's how you can do it in your _layout.js file: