我是React和这些语言的新手。尝试将自定义的谷歌字体(Ubuntu)应用到整个项目。我设法将字体拉到项目中,但我只能在App.js中的简单文本中使用它。
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}
/>
);
}
}
但是我使用这些文本只是为了测试。在正常情况下,我的代码是这样的,当我尝试使用ubuntuRegular字体时,它不起作用。
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}
/>
);
}
}
此外,我如何在App.js之外使用此字体,就像我有一个MenuTitle组件一样:
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'
},
});
而且这也不起作用。如果你能帮忙,我会很高兴的。
7条答案
按热度按时间ruarlubt1#
创建一个名为
hooks
的文件夹,在其中存放App.js
,然后在hooks
文件夹中创建一个名为useFonts.js
的文件在
useFonts.js
中,如下所示现在在
App.js
中这样写6rvt4ljy2#
1.将字体资源添加到项目文件夹exp assets/fonts
1.创建变量字体exp customFonts
1.在App.js上导入,如下所示
cs7cruho3#
将.ttf添加到资产后,您是否尝试过执行以下步骤/font:
或
如果一切正常,您可以在android/app/src/main/assets/fonts中看到新的字体资源,在iOS中可以在Info.plist中看到更改。
最后,重新运行应用程序
在此之后,您可以将您的字体样式放在样式文件中,然后在所有项目中使用它们:
idv4meu84#
添加代码如上所示,然后你可以使用字体在任何网页上喜欢如下。
5cnsuln75#
使用Expo,您通常可以通过两种不同的方式将自定义字体应用于项目:
第一种方法是使用@expo-google-fonts/* 软件包,简单地安装该软件包,如下所示:
yarn add @expo-google-fonts/蒙特塞拉特(安装蒙特塞拉特字体)
然后在你的代码中
使用此目录获取更多帮助:https://directory.vercel.app/
useFont钩子处理其他应答中显示的
Font.loadAsync
逻辑。第二种方法是在本地导入字体。
小提示:
如果你使用的是最新的SDK 45,expo-app-loading已经过时了,应该用expo-splash-screen来代替,Expo docs可以帮你解决这个问题。
isr3a4wc6#
使用Font.loadAsync而不是useFonts挂接如果不想使用useFonts挂接。
6ie5vjzr7#
有了expo,你可以通过使用'expo-google-fonts' liabrary直接使用谷歌字体。
安装命令npx expo安装expo-font @expo-google-fonts/inter
使用Google字体
获取更多帮助访问Expo Docs Expo谷歌字体