|
6 | 6 | content: Fonts | React Native / Expo Starter |
7 | 7 | --- |
8 | 8 |
|
9 | | -With Expo, you can load fonts dynamically using `useFont` hook from `expo-font` library. With this approach, you need to wait for the font to load before showing or hiding your splash screen. |
| 9 | +## Overview |
10 | 10 |
|
11 | | -With the last version of `expo-font` introduced with Expo 50, you can use `expo-font` as a plugin in your `app.config.js` to load fonts natively. |
| 11 | +This starter uses the `expo-font` config plugin to load fonts natively, providing optimal performance and ensuring fonts are available immediately when your app starts. |
12 | 12 |
|
13 | | -To add a custom font you only need to put the font file in the `assets/fonts` and update the expo config by adding the exact file path to the config like the following: |
| 13 | +## Current Setup |
14 | 14 |
|
15 | | -```js title="app.config.js" |
16 | | -import type { ConfigContext, ExpoConfig } from '@expo/config'; |
| 15 | +The template comes pre-configured with the **Inter** font family including 4 weights: |
| 16 | +- Regular (400) |
| 17 | +- Medium (500) |
| 18 | +- SemiBold (600) |
| 19 | +- Bold (700) |
17 | 20 |
|
| 21 | +Inter is configured as the default sans-serif font in the Tailwind CSS theme, so all text uses Inter by default. |
| 22 | + |
| 23 | +## Using Fonts in Your App |
| 24 | + |
| 25 | +### With Tailwind CSS Classes |
| 26 | + |
| 27 | +Since Inter is configured as the default `font-sans`, you can use standard Tailwind font classes: |
| 28 | + |
| 29 | +```tsx |
| 30 | +import { Text } from 'react-native'; |
| 31 | + |
| 32 | +<Text className="font-sans font-normal">Regular Text (400)</Text> |
| 33 | +<Text className="font-sans font-medium">Medium Text (500)</Text> |
| 34 | +<Text className="font-sans font-semibold">SemiBold Text (600)</Text> |
| 35 | +<Text className="font-sans font-bold">Bold Text (700)</Text> |
| 36 | +``` |
| 37 | + |
| 38 | +### With Style Props |
| 39 | + |
| 40 | +You can also use the font directly with React Native style props: |
| 41 | + |
| 42 | +```tsx |
| 43 | +<Text style={{ fontFamily: 'Inter', fontWeight: '400' }}>Regular Text</Text> |
| 44 | +<Text style={{ fontFamily: 'Inter', fontWeight: '500' }}>Medium Text</Text> |
| 45 | +<Text style={{ fontFamily: 'Inter', fontWeight: '600' }}>SemiBold Text</Text> |
| 46 | +<Text style={{ fontFamily: 'Inter', fontWeight: '700' }}>Bold Text</Text> |
| 47 | +``` |
| 48 | + |
| 49 | +## Adding a New Font |
| 50 | + |
| 51 | +To add a new Google Font to your app, follow these steps: |
| 52 | + |
| 53 | +### 1. Install the Font Package |
| 54 | + |
| 55 | +Install the desired font from the `@expo-google-fonts` collection: |
| 56 | + |
| 57 | +```bash |
| 58 | +pnpm add @expo-google-fonts/roboto |
| 59 | +``` |
| 60 | + |
| 61 | +### 2. Update `app.config.ts` |
| 62 | + |
| 63 | +Add the font to your expo-font plugin configuration with platform-specific settings: |
| 64 | + |
| 65 | +```ts title="app.config.ts" |
18 | 66 | export default ({ config }: ConfigContext): ExpoConfig => ({ |
19 | 67 | ...config, |
20 | 68 | plugins: [ |
21 | 69 | [ |
22 | 70 | 'expo-font', |
23 | 71 | { |
24 | | - fonts: ['./assets/fonts/Inter.ttf'], |
| 72 | + ios: { |
| 73 | + fonts: [ |
| 74 | + 'node_modules/@expo-google-fonts/inter/400Regular/Inter_400Regular.ttf', |
| 75 | + 'node_modules/@expo-google-fonts/inter/500Medium/Inter_500Medium.ttf', |
| 76 | + 'node_modules/@expo-google-fonts/inter/600SemiBold/Inter_600SemiBold.ttf', |
| 77 | + 'node_modules/@expo-google-fonts/inter/700Bold/Inter_700Bold.ttf', |
| 78 | + // Add your new font |
| 79 | + 'node_modules/@expo-google-fonts/roboto/400Regular/Roboto_400Regular.ttf', |
| 80 | + 'node_modules/@expo-google-fonts/roboto/700Bold/Roboto_700Bold.ttf', |
| 81 | + ], |
| 82 | + }, |
| 83 | + android: { |
| 84 | + fonts: [ |
| 85 | + { |
| 86 | + fontFamily: 'Inter', |
| 87 | + fontDefinitions: [ |
| 88 | + { |
| 89 | + path: 'node_modules/@expo-google-fonts/inter/400Regular/Inter_400Regular.ttf', |
| 90 | + weight: 400, |
| 91 | + }, |
| 92 | + { |
| 93 | + path: 'node_modules/@expo-google-fonts/inter/500Medium/Inter_500Medium.ttf', |
| 94 | + weight: 500, |
| 95 | + }, |
| 96 | + { |
| 97 | + path: 'node_modules/@expo-google-fonts/inter/600SemiBold/Inter_600SemiBold.ttf', |
| 98 | + weight: 600, |
| 99 | + }, |
| 100 | + { |
| 101 | + path: 'node_modules/@expo-google-fonts/inter/700Bold/Inter_700Bold.ttf', |
| 102 | + weight: 700, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }, |
| 106 | + // Add your new font family |
| 107 | + { |
| 108 | + fontFamily: 'Roboto', |
| 109 | + fontDefinitions: [ |
| 110 | + { |
| 111 | + path: 'node_modules/@expo-google-fonts/roboto/400Regular/Roboto_400Regular.ttf', |
| 112 | + weight: 400, |
| 113 | + }, |
| 114 | + { |
| 115 | + path: 'node_modules/@expo-google-fonts/roboto/700Bold/Roboto_700Bold.ttf', |
| 116 | + weight: 700, |
| 117 | + }, |
| 118 | + ], |
| 119 | + }, |
| 120 | + ], |
| 121 | + }, |
25 | 122 | }, |
26 | 123 | ], |
27 | 124 | ], |
28 | 125 | }); |
29 | 126 | ``` |
30 | 127 |
|
31 | | -Next, Make sure to add your new font to Tailwind CSS config to use it with `className` |
| 128 | +### 3. Update Tailwind Theme (Optional) |
| 129 | + |
| 130 | +If you want to use the font with Tailwind CSS, add it to your theme in `src/global.css`: |
| 131 | + |
| 132 | +```css title="src/global.css" |
| 133 | +@theme { |
| 134 | + /* Font families */ |
| 135 | + --font-family-sans: 'Inter', ui-sans-serif, system-ui, sans-serif; |
| 136 | + --font-family-roboto: 'Roboto', ui-sans-serif, system-ui, sans-serif; |
| 137 | + |
| 138 | + /* ... other theme config */ |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### 4. Run Prebuild |
32 | 143 |
|
33 | | -```js title="tailwind.config.js" |
34 | | -const colors = require('./src/components/ui/theme/colors'); |
| 144 | +After updating the configuration, run prebuild to apply the changes: |
35 | 145 |
|
36 | | -/** @type {import('tailwindcss').Config} */ |
37 | | -module.exports = { |
38 | | - // NOTE: Update this to include the paths to all of your component files. |
39 | | - content: ['./src/**/*.{js,jsx,ts,tsx}'], |
40 | | - presets: [require('nativewind/preset')], |
41 | | - theme: { |
42 | | - extend: { |
43 | | - fontFamily: { |
44 | | - inter: ['Inter'], |
| 146 | +```bash |
| 147 | +pnpm run prebuild |
| 148 | +``` |
| 149 | + |
| 150 | +Then rebuild your app: |
| 151 | + |
| 152 | +```bash |
| 153 | +pnpm run ios # or pnpm run android |
| 154 | +``` |
| 155 | + |
| 156 | +## Using Local Font Files |
| 157 | + |
| 158 | +If you prefer to use local font files instead of npm packages: |
| 159 | + |
| 160 | +### 1. Add Font Files |
| 161 | + |
| 162 | +Place your font files in the `assets/fonts` directory: |
| 163 | + |
| 164 | +``` |
| 165 | +assets/fonts/ |
| 166 | + ├── CustomFont-Regular.ttf |
| 167 | + └── CustomFont-Bold.ttf |
| 168 | +``` |
| 169 | + |
| 170 | +### 2. Update `app.config.ts` |
| 171 | + |
| 172 | +Reference the local font files in your configuration: |
| 173 | + |
| 174 | +```ts title="app.config.ts" |
| 175 | +plugins: [ |
| 176 | + [ |
| 177 | + 'expo-font', |
| 178 | + { |
| 179 | + ios: { |
| 180 | + fonts: [ |
| 181 | + './assets/fonts/CustomFont-Regular.ttf', |
| 182 | + './assets/fonts/CustomFont-Bold.ttf', |
| 183 | + ], |
| 184 | + }, |
| 185 | + android: { |
| 186 | + fonts: [ |
| 187 | + { |
| 188 | + fontFamily: 'CustomFont', |
| 189 | + fontDefinitions: [ |
| 190 | + { |
| 191 | + path: './assets/fonts/CustomFont-Regular.ttf', |
| 192 | + weight: 400, |
| 193 | + }, |
| 194 | + { |
| 195 | + path: './assets/fonts/CustomFont-Bold.ttf', |
| 196 | + weight: 700, |
| 197 | + }, |
| 198 | + ], |
| 199 | + }, |
| 200 | + ], |
45 | 201 | }, |
46 | | - colors, |
47 | 202 | }, |
48 | | - }, |
49 | | - plugins: [], |
50 | | -}; |
| 203 | + ], |
| 204 | +], |
51 | 205 | ``` |
52 | 206 |
|
| 207 | +### 3. Run Prebuild |
| 208 | + |
| 209 | +```bash |
| 210 | +pnpm run prebuild |
| 211 | +``` |
| 212 | + |
| 213 | +## Important Notes |
| 214 | + |
| 215 | +:::caution |
| 216 | +Font changes require running `expo prebuild` and rebuilding your app. They won't take effect with just a hot reload or app restart. |
| 217 | +::: |
| 218 | + |
53 | 219 | :::info |
54 | | -As we are linking font natively you need to run `expo prebuild` and then `expo ios` or `expo android` to use the new font. |
| 220 | +- **iOS**: Font family names are automatically extracted from the font files |
| 221 | +- **Android**: You explicitly define the `fontFamily` name and weight mappings |
| 222 | +- Using this approach, you can use `fontFamily: 'FontName'` with `fontWeight` consistently across both platforms |
55 | 223 | ::: |
56 | 224 |
|
57 | | -More details about adding fonts with Tailwind CSS can be found in the [Nativewind documentation](https://www.nativewind.dev/v4/tailwind/typography/font-family). |
| 225 | +## Resources |
| 226 | + |
| 227 | +- [Expo Fonts Documentation](https://docs.expo.dev/develop/user-interface/fonts/) |
| 228 | +- [Google Fonts Expo Packages](https://github.com/expo/google-fonts) |
| 229 | +- [Tailwind CSS Font Family](https://tailwindcss.com/docs/font-family) |
0 commit comments