简体   繁体   English

React Native - Expo:fontFamily 'SimpleLineIcons' 不是系统字体,尚未通过 Font.loadAsync 加载

[英]React Native - Expo: fontFamily 'SimpleLineIcons' is not a system font and has not been loaded through Font.loadAsync

So I get this error on Android device/emulator:所以我在 Android 设备/模拟器上收到此错误:

模拟器上的错误信息

On iOS on the other hand, it's compiling just fine and the simple-line-icons are displayed properly.另一方面,在 iOS 上,它编译得很好,并且simple-line-icons显示正确。

I'm running the latest version of expo.我正在运行最新版本的世博会。

My package.json:我的 package.json:

{
  "name": "FamScore3",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "react-native-scripts": "1.14.0",
    "jest-expo": "^31.0.0",
    "react-test-renderer": "16.3.1"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "jest"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "@firebase/auth": "^0.7.6",
    "@firebase/database": "^0.3.6",
    "axios": "^0.18.0",
    "metro-react-native-babel-preset": "^0.45.0",
    "expo": "^31.0.4",
    "firebase": "^5.5.1",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-31.0.1.tar.gz",
    "react-native-elements": "^0.19.1",
    "react-native-material-dropdown": "^0.11.1",
    "react-native-router-flux": "^4.0.1",
    "react-redux": "^5.0.7",
    "redux": "^4.0.0",
    "redux-thunk": "^2.3.0"
  }
}

My app.json:我的 app.json:

{
  "expo": {
    "sdkVersion": "31.0.0"
  }
}

My Font.loadAsync method in App.js as implemented in the docs:我在 App.js 中的 Font.loadAsync 方法在文档中实现:

export default class App extends Component {

  state = {
    fontLoaded: false
  }

  async componentDidMount() {
    try {

      await Font.loadAsync({
        amaticBold: require('./assets/fonts/amaticSC-Bold.ttf'),
        indieFlower: require('./assets/fonts/indieFlower.ttf'),
        'Material Icons': require('@expo/vector-icons/fonts/MaterialIcons.ttf'),
        'simple-line-icons': require('@expo/vector-icons/fonts/SimpleLineIcons.ttf')

      })

      this.setState({ fontLoaded: true })

    } catch (error) {
      console.log('error loading fonts', error);
    }

  }

  render() {
    const store = createStore(reducers, {}, applyMiddleware(ReduxThunk))

    if (!this.state.fontLoaded) {
      return <AppLoading />
    }

    return (
      <Provider store={store}>
        <Router />
      </Provider> 
    )
  }
}

Thanks a lot in advance.非常感谢。 Any help would be much appreciated!任何帮助将非常感激! I've been stuck for a while.我被困了一段时间。

Okay, so I finally managed to solve this issue.好的,所以我终于设法解决了这个问题。

The problem was that iOS and Android apparently require different names for the fonts you want to load.问题在于 iOS 和 Android 显然需要为您要加载的字体使用不同的名称。

The simple-line-icons that i loaded in only worked for iOS and so on Android i got this error: fontFamily 'SimpleLineIcons' is not a system font and has not been loaded through Font.loadAsync.我加载的简单线条图标仅适用于 iOS 等 Android 我收到此错误:fontFamily 'SimpleLineIcons' 不是系统字体,尚未通过 Font.loadAsync 加载。

I ended up loading both the simple-line-icons and the SimpleLineIcons, pointing to the same directory like so:我最终加载了 simple-line-icons 和 SimpleLineIcons,指向同一个目录,如下所示:

componentDidMount() {
    this.loadAssetsAsync()
  }

  loadAssetsAsync = async () => {
    await Font.loadAsync({
      amaticBold: require('./assets/fonts/amaticSC-Bold.ttf'),
      indieFlower: require('./assets/fonts/indieFlower.ttf'),
      'Material Icons': require('@expo/vector-icons/fonts/MaterialIcons.ttf'),
      'MaterialIcons': require('@expo/vector-icons/fonts/MaterialIcons.ttf'),
      'SimpleLineIcons': require('@expo/vector-icons/fonts/SimpleLineIcons.ttf'),
      'simple-line-icons': require('@expo/vector-icons/fonts/SimpleLineIcons.ttf')
    })
    this.setState({ fontLoaded: true })
  }

  render() {
    const store = createStore(reducers, {}, applyMiddleware(ReduxThunk))

    if (!this.state.fontLoaded) {
      return <AppLoading />
    }

    return (
      <Provider store={store}>
        <Router />
      </Provider>
    )
  }

This admittedly ugly solution solved my issue for now.这个公认的丑陋解决方案暂时解决了我的问题。 Make sure you really type exactly like the error tells you to.确保你真的像错误告诉你的那样输入。 If the error is: fontFamily 'MyAwesomeFont' is not a system font... Then you really need to name it:如果错误是:fontFamily 'MyAwesomeFont' is not a system font... 那么你真的需要给它命名:

Font.loadAsync({
    'MyAwesomeFont': require('./path/to/MyAwesomeFont.ttf')
})

Hope this helps anyone out there.希望这可以帮助那里的任何人。

I had the same issue and I found that changing the font name to the one you named can solve issue:我遇到了同样的问题,我发现将字体名称更改为您命名的名称可以解决问题:

// Before
'my-awesome-font': require('./path/to/my-awesome-font.ttf')

// After
'MyAwesomeFont': require('./path/to/my-awesome-font.ttf')

It may be the dash sign that cannot be used.可能是不能使用的破折号。

I had the same problem and wasted a lot of time on this error.我遇到了同样的问题,并在这个错误上浪费了很多时间。 This is 2021 and there is a better way to do the same thing so instead of using the code below这是 2021 年,有更好的方法来做同样的事情,所以不要使用下面的代码

Font.loadAsync({
    'MyAwesomeFont': require('./path/to/MyAwesomeFont.ttf')
})

The correct way to do this is正确的做法是

import { useFonts } from 'expo-font';
import AppLoading from 'expo-app-loading';

  let [fontsLoaded] = useFonts({
    'open-sans': require('./assets/fonts/OpenSans-Regular.ttf'),
    'open-sans-bold': require('./assets/fonts/OpenSans-Bold.ttf'),
  });
  if (!fontsLoaded) {
    return <AppLoading />;
  }

If you want further explanation check the official docs here如果您想进一步解释,请在此处查看官方文档

I faced this error when I was using @react-native-render-html , where it was rendering the Html generated from other cms, and this causes the font family problem if the content creator used a custom font.我在使用 @react-native-render-html 时遇到了这个错误,它渲染了从其他 cms 生成的 Html,如果内容创建者使用自定义字体,这会导致字体系列问题。

so to solve this I used the ignoredStyles feature in html renderer, and then set the baseFontStyle to make a uniform look of the text contents.所以为了解决这个问题,我在 html 渲染器中使用了ignoreStyles功能,然后设置了baseFontStyle以使文本内容具有统一的外观。

code:代码:

import HTML from "react-native-render-html"`

<HTML ignoredStyles={['fontFamily']} baseFontStyle={[fontFamily: 'nunito-regular', fontSize: 16]} />

If you are coming from 'The net ninja' Tuto here is the solution如果您来自“网络忍者”Tuto,这里是解决方案

Home.js主页.js

import React, { useState } from 'react';
import {StyleSheet, View, Text } from 'react-native';

export default function Home() {
    return (
        <View style={styles.container}>
            <Text style={styles.titleText}>Home Screen</Text>
        </View>
    );
}

// https://docs.expo.dev/guides/using-custom-fonts/
const styles = StyleSheet.create({
    container: {
        padding: 24,
    },
    titleText:{
        fontFamily: 'Nunito-Bold',
        fontSize: 24,
    }
})

App.js应用程序.js

import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Home from './screens/home';

import { useFonts } from 'expo-font';
import AppLoading from 'expo-app-loading';

export default function App() {
  // const [fontsLoaded, setFontsLoaded] = useState(false);

  const [fontsLoaded] = useFonts({
    'Nunito-Regular': require('./assets/fonts/Nunito-Regular.ttf'),
    'Nunito-Bold': require('./assets/fonts/Nunito-Bold.ttf'),
  });

  if (!fontsLoaded) {
    return (
      <AppLoading />
    )
  } else {
    return (
      <Home />
    );
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM