【发布时间】:2021-04-16 16:49:05
【问题描述】:
我正在通过创建一些项目来学习 React Native。
我卡住了,无法加载自定义字体。我遵循了来自世博会文档的this 指南。但我遇到了一个错误
fontFamily "raleway-bold" 不是系统字体,尚未加载 通过 Font.loadAsync。
然后我在我的代码中使用了这个Font.loadAsync,但我仍然得到同样的错误。
有什么建议吗?
import React, { useState } from 'react';
import Home from './screens/Home';
import AppLoading from 'expo-app-loading';
import * as Font from 'expo-font';
const getFonts = () =>
Font.loadAsync({
'raleway-regular': require('./assets/fonts/Raleway-Regular.ttf'),
'raleway-bold': require('./assets/fonts/Raleway-Bold.ttf'),
});
export default function App() {
const [fontsLoaded, setFontsLoaded] = useState(false);
if (fontsLoaded) {
return <Home />;
} else {
return (
<AppLoading
startAsync={getFonts}
onFinish={() => setFontsLoaded(true)}
onError={console.warn}
/>
);
}
}
【问题讨论】:
-
能否发一个
AppLoading组件的代码? -
我没有
AppLoading组件的自定义代码 -
我是否添加了一些自定义代码?很抱歉新人反应原生
-
哦,我明白了。对不起。我没有意识到它是一个博览会组件。尝试像这样使您的
getFonts方法异步:const getFonts = async () => { //your code }. -
还是同样的错误
标签: reactjs react-native fonts