I'm new to React and those languages. Trying to apply a custom google font(Ubuntu) to whole project. I managed to pull the font into the project, but I can only use it in simple texts in App.js. Like this:
import React, {useState} from 'react';
import './firebase/index';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { ThemeProvider } from 'react-native-elements';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import { BLUE } from './common/colors';
import Dashboard from './screens/Dashboard';
import Login from './screens/Login';
import Accounts from './screens/Accounts';
import Cards from './screens/Cards';
import Credits from './screens/Credits';
import Insurances from './screens/Insurances';
import Investments from './screens/Investments';
import MoneyTransfers from './screens/MoneyTransfers';
import OtherOperations from './screens/OtherOperations';
import Payments from './screens/Payments';
import * as Font from "expo-font";
import Apploading from "expo-app-loading";
import { StyleSheet, Text, View } from "react-native";
const Drawer = createDrawerNavigator();
const getFonts = () =>
Font.loadAsync({
limelight: require("./assets/fonts/Ubuntu-Regular.ttf"),
indie: require("./assets/fonts/Ubuntu-BoldItalic.ttf"),
});
export default function App() {
const [fontsloaded, setFontsLoaded] = useState(false);
if (fontsloaded){
return (
<View>
<Text
style={{ fontFamily: "limelight" }}>
Hello World
</Text>
<Text
style={{ fontFamily: "limelight" }}>
Hello World
</Text>
<Text
>
Hello World
</Text>
<Text
style={{ fontFamily: "indie" }}>
Hello World
</Text>
<Text
style={{ fontFamily: "limelight" }}>
Hello World
</Text>
<Text
>
Hello World
</Text>
<Text
style={{ fontFamily: "indie" }}>
Hello World
</Text>
<Text
style={{ fontFamily: "indie" }}>
Hello World
</Text>
<Text
>
Hello World
</Text>
<Text
>
Hello World
</Text>
<Text
style={{ fontFamily: "limelight" }}>
Hello World
</Text>
<Text
>
Hello World
</Text>
</View>
);
}
else {
return (
<Apploading
startAsync={getFonts}
onFinish={() => {
setFontsLoaded(true);
}}
onError={console.warn}
/>
);
}
}
But I used those text just for test. In normal my code is like this and when I try to use ubuntuRegular font it is not working.
import React, {useState} from 'react';
import './firebase/index';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { ThemeProvider } from 'react-native-elements';
import { createDrawerNavigator } from '@react-navigation/drawer';
import { NavigationContainer } from '@react-navigation/native';
import { BLUE } from './common/colors';
import Dashboard from './screens/Dashboard';
import Login from './screens/Login';
import Accounts from './screens/Accounts';
import Cards from './screens/Cards';
import Credits from './screens/Credits';
import Insurances from './screens/Insurances';
import Investments from './screens/Investments';
import MoneyTransfers from './screens/MoneyTransfers';
import OtherOperations from './screens/OtherOperations';
import Payments from './screens/Payments';
import * as Font from "expo-font";
import Apploading from "expo-app-loading";
import { StyleSheet, Text, View } from "react-native";
const Drawer = createDrawerNavigator();
const getFonts = () =>
Font.loadAsync({
limelight: require("./assets/fonts/Ubuntu-Regular.ttf"),
indie: require("./assets/fonts/Ubuntu-BoldItalic.ttf"),
});
export default function App() {
const [fontsloaded, setFontsLoaded] = useState(false);
if (fontsloaded){
return (
<SafeAreaProvider>
<ThemeProvider>
<NavigationContainer>
<Drawer.Navigator
drawerType="slide"
initialRouteName="Login"
screenOptions={{
headerShown: true,
headerStyle: {
backgroundColor: BLUE,
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
fontFamily: 'ubuntuRegular'
},
}}
>
{/* Dashboard a ayri header gecilebilir
https://reactnavigation.org/docs/headers#replacing-the-title-with-a-custom-component */}
<Drawer.Screen
name="Login"
component={Login}
options={{ headerShown: false, title: 'Login' }}
/>
<Drawer.Screen
name="Dashboard"
component={Dashboard}
options={{ title: 'Anasayfa' }}
/>
<Drawer.Screen
name="Accounts"
component={Accounts}
options={{ title: 'Hesaplarım' }}
/>
<Drawer.Screen
name="Cards"
component={Cards}
options={{ title: 'Kartlarım' }}
/>
<Drawer.Screen
name="Money Transfers"
component={MoneyTransfers}
options={{ title: 'Para Transferleri' }}
/>
<Drawer.Screen
name="Investments"
component={Investments}
options={{ title: 'Yatırımlar' }}
/>
<Drawer.Screen
name="Payments"
component={Payments}
options={{ title: 'Ödemeler' }}
/>
<Drawer.Screen
name="Credits"
component={Credits}
options={{ title: 'Krediler' }}
/>
<Drawer.Screen
name="Insurances"
component={Insurances}
options={{ title: 'Sigortalar' }}
/>
<Drawer.Screen
name="Other Operations"
component={OtherOperations}
options={{ title: 'Diğer İşlemler' }}
/>
</Drawer.Navigator>
</NavigationContainer>
</ThemeProvider>
</SafeAreaProvider>
);
}
else {
return (
<Apploading
startAsync={getFonts}
onFinish={() => {
setFontsLoaded(true);
}}
onError={console.warn}
/>
);
}
}
Also how can I use this font outside of the App.js like I have a MenuTitle component:
import React, {useState} from 'react';
import { StyleSheet, Text } from 'react-native';
import * as Font from "expo-font";
import Apploading from "expo-app-loading";
const getFonts = () =>
Font.loadAsync({
limelight: require("../assets/fonts/Ubuntu-Regular.ttf"),
indie: require("../assets/fonts/Ubuntu-BoldItalic.ttf"),
});
const MenuTitle = ({ text, textStyles = {} }) => {
return <Text style={[styles.title, textStyles]}>{text}</Text>;
};
export default MenuTitle;
const styles = StyleSheet.create({
title: {
fontSize: 20,
fontWeight: 'bold',
paddingHorizontal: 20,
fontFamily: 'ubuntuRegular'
},
});
Also this is not working. I will be glad if you can help.
Create a folder called hooks where your App.js is located. Then inside hooks folder create a file called useFonts.js
Inside useFonts.js write like this
import * as Font from 'expo-font';
export default useFonts = async () =>
await Font.loadAsync({
limelight: require('../assets/fonts/Ubuntu-Regular.ttf'),
indie: require('../assets/fonts/Ubuntu-BoldItalic.ttf'),
});
Now in your App.js write like this
import * as Font from 'expo-font';
import AppLoading from 'expo-app-loading';
import React, { useState } from 'react';
import useFonts from './hooks/useFonts';
export default function App() {
const [IsReady, SetIsReady] = useState(false);
const LoadFonts = async () => {
await useFonts();
};
if (!IsReady) {
return (
<AppLoading
startAsync={LoadFonts}
onFinish={() => SetIsReady(true)}
onError={() => {}}
/>
);
}
return <View styles={styles.container}>{/* Rest of the Code Here */}</View>;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With