Working with Custom Fonts and Icons
1. Installing Custom Fonts
Example: Adding a custom font to a React Native project
// 1. Add font files to your project
// Place font files in: assets/fonts/
// 2. Update your app.json (for Expo projects)
{
"expo": {
"fonts": [
"assets/fonts/MyCustomFont-Regular.ttf",
"assets/fonts/MyCustomFont-Bold.ttf"
]
}
}
// 3. For bare React Native projects, update Info.plist (iOS)
<key>UIAppFonts</key>
<array>
<string>assets/fonts/MyCustomFont-Regular.ttf</string>
<string>assets/fonts/MyCustomFont-Bold.ttf</string>
</array>
// 4. Use the font in your component
import { Text } from 'react-native';
<Text style={{ fontFamily: 'MyCustomFont-Regular' }}>
This text uses a custom font
</Text>
| Step | Description | Notes |
|---|---|---|
| 1. Add font files | assets/fonts/MyCustomFont-Regular.ttf |
Place font files in a dedicated assets folder |
| 2. Configure for Expo | app.json |
Add font paths to the fonts array in expo |
| 3. Configure for bare project (iOS) | Info.plist |
Add UIAppFonts array with font file paths |
| 4. Use in component | fontFamily |
Apply font family in style prop using the font name |
Note: Font files must be in a supported format (e.g.,
.ttf, .otf). For Android, font files should be placed in android/app/src/main/assets/fonts/.
Warning: Custom fonts may increase app size. Consider using system fonts for better performance.
2. Linking Font Files
Example: Adding a Custom Font
import { Font } from 'expo-font';
import { useEffect, useState } from 'react';
export default function App() {
const [fontsLoaded, setFontsLoaded] = useState(false);
useEffect(() => {
loadFonts();
}, []);
const loadFonts = async () => {
await Font.loadAsync({
'custom-font': require('./assets/fonts/CustomFont.ttf'),
});
setFontsLoaded(true);
};
if (!fontsLoaded) return <AppLoading />;
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ fontFamily: 'custom-font' }}>Hello World!</Text>
</View>
);
}
| Property | Description | Values |
|---|---|---|
| fontFamily | Name of the font family to use | 'custom-font' |
| fontFile | Path to the font file | require('./assets/fonts/CustomFont.ttf') |
Note: Font files must be added to the project's assets directory and referenced correctly in the code.
3. Using Custom Fonts
| Property | Description | Values |
|---|---|---|
| fontFamily | Specifies the font family to use | 'Roboto', 'Arial', 'CustomFont' |
| fontSize | Sets the size of the font | 16, 20 |
| fontWeight | Defines the weight of the font | 'normal', 'bold', 400, 700 |
Example: Applying Custom Font
{'import React from 'react';\nimport { Text, StyleSheet } from 'react-native';\n\nconst CustomFontText = () => {\n return (\n <Text style={styles.customFont}>\n Hello, Custom Font!\n </Text>\n );\n};\n\nconst styles = StyleSheet.create({\n customFont: {\n fontFamily: 'Roboto',\n fontSize: 18,\n fontWeight: 'bold'\n }\n});\n\nexport default CustomFontText;'}
Note: Custom fonts must be added to the project and configured in the app's native configuration (e.g.,
Info.plist on iOS or AndroidManifest.xml on Android).
Warning: Font files must be properly linked and included in the project build for them to render correctly on both platforms.
4. Installing Vector Icons
| Icon Library | Installation Command | Notes |
|---|---|---|
| FontAwesome | npm install react-native-vector-icons |
Includes FontAwesome icons by default |
| MaterialIcons | npm install react-native-vector-icons |
Includes MaterialIcons by default |
| Ionicons | npm install react-native-vector-icons |
Includes Ionicons by default |
Note: For iOS, you may need to run
cd ios && pod install after installing the package.Warning: On Android, you might need to manually link the library or use
react-native link if auto-linking doesn't work.Installation Steps
- Install the package using npm or yarn
- For iOS, run
pod installin theiosdirectory - For Android, ensure auto-linking works or manually link the library
5. Using Icon Components
Example: Using react-native-vector-icons
import Icon from 'react-native-vector-icons/MaterialIcons';
<Icon name="home" size={30} color="#000" />
| Property | Values | Description |
|---|---|---|
| name | string |
Icon name from the icon set |
| size | number |
Icon size in pixels |
| color | string |
Icon color in hex or named color |
| style | object |
Additional styles to apply |
Note: Ensure the icon library is installed and linked properly for native support.
Comparison: Icon Libraries
- react-native-vector-icons: Supports multiple icon sets (Material, FontAwesome, etc.)
- react-native-icons: Limited set but lightweight
- react-native-ico: Large icon library with SVG support
6. Creating Custom Icon Sets
Example: Basic Icon Component
{`import React from 'react';
import { View, Text } from 'react-native';
const CustomIcon = ({ name, size = 24, color = '#000' }) => {
return (
<View style={{ width: size, height: size }}>
<Text style={{ fontSize: size, color }}>{name}</Text>
</View>
);
};
export default CustomIcon;`}
| Property | Description | Values |
|---|---|---|
| name | Icon identifier | string |
| size | Icon size in pixels | number |
| color | Icon color | string |
Note: For production apps, use vector-based icons (SVG) or image assets for better scalability and performance.
Example: Using Vector Icons
{`import React from 'react';
import { View } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialIcons';
const IconExample = () => {
return (
<View style={{ flexDirection: 'row', padding: 10 }}>
<Icon name="home" size={30} color="#000" />
<Icon name="favorite" size={30} color="#f00" />
<Icon name="settings" size={30} color="#00f" />
</View>
);
};
export default IconExample;`}
7. Styling Icons
| Property | Values | Description |
|---|---|---|
| color | string |
Color of the icon (supports all CSS color formats) |
| size | number |
Size of the icon in pixels |
| style | object |
Additional styles to apply to the icon container |
8. Using Icons in Buttons and Tabs
| Property | Description | Example |
|---|---|---|
| icon | Icon component to display in the button or tab | <Icon name="home" /> |
| iconPosition | Position of the icon relative to text (left, right) | iconPosition: 'right' |
| size | Size of the icon (small, medium, large) | size: 'medium' |
Example: Button with Icon
import { Button } from 'react-native-elements';
import Icon from 'react-native-vector-icons/MaterialIcons';
<Button
title="Home"
icon={
<Icon
name="home"
size={20}
color="white"
/>
}
iconRight
/>
Example: Tab with Icon
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/MaterialIcons';
const Tab = createBottomTabNavigator();
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Home') {
iconName = focused ? 'home' : 'home-outline';
} else if (route.name === 'Profile') {
iconName = focused ? 'person' : 'person-outline';
}
return <Icon name={iconName} size={size} color={color} />;
},
})}
>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Profile" component={ProfileScreen} />
</Tab.Navigator>
Note: Ensure that the icon library is properly installed and linked for native support (e.g.,
react-native-vector-icons).
9. Implementing Icon Fonts
Example: Adding Icon Font to Project
// Install react-native-vector-icons
npm install react-native-vector-icons
// For iOS, run:
cd ios && pod install
// For Android, no additional setup needed
| Icon Library | Installation Command | Usage Example | Notes |
|---|---|---|---|
| Entypo | npm install react-native-vector-icons |
<Icon name="home" size={30} color="#900" /> |
Includes 200+ icons |
| FontAwesome | npm install react-native-vector-icons |
<Icon name="heart" size={25} color="red" /> |
Popular choice with 1500+ icons |
| MaterialIcons | npm install react-native-vector-icons |
<Icon name="favorite" size={20} color="#000" /> |
Google Material Design icons |
Note: On iOS, you must run
pod install after installing the library.
Example: Using Custom Icon Font
import Icon from 'react-native-vector-icons/FontAwesome';
const MyComponent = () => {
return (
<View>
<Icon name="heart" size={30} color="#f00" />
<Icon name="star" size={25} color="#ff0" />
</View>
);
};
Example: Custom Icon Size and Color
import Icon from 'react-native-vector-icons/MaterialIcons';
const CustomIcon = () => {
return (
<Icon
name="favorite"
size={40}
color="#ff5722"
onPress={() => console.log('Favorite tapped')}
/>
);
};
10. Optimizing Font Loading
Example: Preloading Fonts with react-native-fonts
import { Font } from 'expo-font';
import { useEffect, useState } from 'react';
const App = () => {
const [fontsLoaded, setFontsLoaded] = useState(false);
useEffect(() => {
const loadFonts = async () => {
await Font.loadAsync({
'Roboto-Bold': require('./assets/fonts/Roboto-Bold.ttf'),
'Roboto-Regular': require('./assets/fonts/Roboto-Regular.ttf'),
});
setFontsLoaded(true);
};
loadFonts();
}, []);
if (!fontsLoaded) return <Text>Loading fonts...</Text>;
return <Text style={{ fontFamily: 'Roboto-Bold' }}>Hello World</Text>;
};
| Property | Description | Notes |
|---|---|---|
| Font.loadAsync() | Asynchronously loads custom fonts | Must be called before using the font |
| fontFamily | Applies font to text components | Must match the key used in loadAsync |
Note: On Android, fonts must be placed in
android/app/src/main/assets/fonts and on iOS in ios/YourApp/Fonts.
Warning: Loading too many fonts can increase app size and startup time.