I am able to use inline styling to use an imported font in an Expo project. However, for the sake of tidiness, I wanted to put all of my styling into a separate file from the code. I am using the react-native StyleSheet library for my styling. It seems that as soon as I move the style with the custom font into another file, the font can no longer be accessed by the StyleSheet.
I have tried importing the font with npx react-native-asset
, the useFonts hook, and the loadAsync() function, and all give the same result.
I tried the solution of waiting for the fonts to load before rendering the components (suggested here), but had the same result again.
I understand I could make the StyleSheet within the component file, but I want it to be reusable across multiple components.
styles.tsx
import { StyleSheet } from "react-native";
export default StyleSheet.create({
customText: {
fontFamily: "SpicyRice",
color: "red",
},
});
MyComponent.tsx
import styles from "./styles";
export default (
<View>
<Text style={{fontFamily: "SpicyRice"}}>
THIS IS WORKING WITH THE CUSTOM FONT.
</Text>
<Text style={styles.customText}>
THIS IS NOT USING THE CUSTOM FONT.
HOWEVER, IT IS RED, SO I KNOW IT IS USING THE STYLESHEET.
</Text>
</View>
);
react-native.config.js
module.exports = {
project: {
ios: {},
android: {},
},
assets: ["./fonts"],
};