I’m trying to make my first app using React Native and Tailwind. I imported the Rajdhani font family to use in this app, but for some reason only Rajdhani Bold actually shows up in the Expo preview on my IOS device that I’m testing on.
this is my index.js file:
import { StatusBar } from "expo-status-bar";
import { Text, View} from "react-native";
import { Link } from "expo-router";
export default function App(){
return(
<View className="flex-1 items-center justify-center bg-white">
<Text className="text-3xl font-rSemiBold">Test test</Text>
<StatusBar style="auto" />
<Link href="/sign-in" style={{color: 'blue'}}>Proceed to Login</Link>
</View>
);
}
This is my tailwind.config.js:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./app/**/*.{js,jsx,ts,tsx}", "./components/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {
colors: {
primary: '#D0E358'
}
},
fontFamily: {
rRegular: ["Rajdhani-Regular", "sans-serif"],
rBold: ["Rajdhani-Bold", "sans-serif"],
rSemiBold: ["Rajdhani-SemiBold", "sans-serif"],
rLight: ["Rajdhani-Light", "sans-serif"],
rMedium: ["Rajdhani-Medium", "sans-serif"]
}
},
plugins: [],
}
and I’ve also attached an image of my folder structure:screenshot
if I replace rSemiBold with rBold, then it will work as intended, but any of the other members of the font family do not. I’ve tried messing around with the config file by adding single quotes around my custom font names or inside the double quotes but nothing has worked. The file names are correct too and I checked the ttf files to make sure there was nothing wrong with them
2
Answers
You need to import the fonts using the useFonts hook from expo in your _layout.tsx file, perhaps you can share a snapshot of your layout file
To ensure your custom Rajdhani fonts work correctly in your React Native app using Tailwind CSS, you need to follow several key steps. Here’s a comprehensive guide to resolving the issue:
Step 1: Load Fonts Using Expo Font
In your index.js or the main entry point of your app, use the useFonts hook from expo-font to load your custom fonts. Ensure the font files are located in the correct directory within your project.
Here’s an updated version of your index.js file:
Step 3: Ensure Tailwind Configuration is Correct
Ensure that your Tailwind CSS configuration properly maps to the fonts you loaded. Here’s your tailwind.config.js with the correct font names:
Step 4: Verify Font File Paths
Double-check that the paths to your font files are correct. They should be relative to the location of your index.js file or where you are importing them.
For example, if your fonts are in the assets/fonts directory, ensure the paths are correctly referenced like this:
Step 5: Restart Your Development Server
After making these changes, restart your development server to ensure all changes are picked up:
Additional Troubleshooting
Final Check
After following these steps, your custom fonts should load correctly. You can verify this by checking if all different styles (Regular, Bold, SemiBold, Light, Medium) are displayed as expected in your app.