【问题标题】:fontFamily is not a system font and has not been loaded through Font.loadAsync But with Font.loadAsyncfontFamily 不是系统字体,没有通过 Font.loadAsync 加载,而是通过 Font.loadAsync
【发布时间】: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 () =&gt; { //your code }.
  • 还是同样的错误

标签: reactjs react-native fonts


【解决方案1】:

这是一个 expo 客户端和 AppLoading 错误GitHub issue

您可以尝试另一种方法,而不是像下面那样使用 AppLoading

  useEffect(() => {
    async function getFonts() {
      await fetchFonts();
      setFontLoaded(true);
    }
    getFonts();
  }, []);
 
  if (!fontLoaded) {
    return (
      <View>
        <Text>Loading...</Text>
      </View>
    );
  }

【讨论】:

  • 感谢您的回答。会尝试更新你。
  • 欢迎@vajad57,请告诉我,如果可行,您可以投票赞成。
猜你喜欢
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
相关资源
最近更新 更多