简体   繁体   English

世博fontello不加载

[英]Expo fontello don't load

I'm trying to load a fontello icons in my project, but if I try to load in App.js, it send "false' and the app don't load:我正在尝试在我的项目中加载 fontello 图标,但是如果我尝试在 App.js 中加载,它会发送“false”并且应用程序不会加载:

 import React, {useState} from "react"; import AppLoading from "expo-app-loading"; import { ThemeProvider } from "styled-components"; import theme from "./src/globalStyles/theme"; import Routes from "./src/routes"; import { useFonts, Roboto_400Regular, Roboto_500Medium, Roboto_700Bold, } from "@expo-google-fonts/roboto"; import { Fontello } from "./assets/fonts/fontello.ttf"; export default function App() { const [fontsLoaded] = useFonts({ Roboto_400Regular, Roboto_500Medium, Roboto_700Bold, Fontello, }); console.log(fontsLoaded) if (!fontsLoaded) { return <AppLoading />; } return ( <ThemeProvider theme={theme}> <Routes /> </ThemeProvider> ); }

And if I comment the line with Fontello, it load the app but I got the following error:如果我用 Fontello 注释该行,它会加载应用程序,但出现以下错误:
fontFamily "fontello" is not a system font and has not been loaded through Font.loadAsync. fontFamily "fontello" 不是系统字体,还没有通过 Font.loadAsync 加载。

If you intended to use a system font, make sure you typed the name correctly and that it is supported by your device operating system.如果您打算使用系统字体,请确保您输入的名称正确并且您的设备操作系统支持它。

If this is a custom font, be sure to load it with Font.loadAsync.如果这是自定义字体,请务必使用 Font.loadAsync 加载它。

Install expo-font安装expo-font

expo install expo-font

Create a folder called hooks where you App.js is located.App.js所在的位置创建一个名为hooks的文件夹。

Inside hooks folder create a file called useFonts.js paste this codehooks文件夹中创建一个名为useFonts.js的文件,粘贴此代码

useFonts.js

import * as Font from 'expo-font';
import { Roboto_400Regular } from '@expo-google-fonts/roboto';

const useFonts = async () => {
  await Font.loadAsync({
    Roboto: Roboto_400Regular,
    Fontello: require('../assets/fonts/fontello.ttf'),
    // All Other Fonts
  });
};

export default useFonts;

Then in your App.js paste this code然后在您的App.js粘贴此代码

import React, { useState } from 'react';
import AppLoading from 'expo-app-loading';
import { ThemeProvider } from 'styled-components';

import theme from './src/globalStyles/theme';
import Routes from './src/routes';
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={(error) => console.log(error)}
      />
    );
  }

   return (
     <ThemeProvider theme={theme}>
       <Routes />
     </ThemeProvider>
   );
}

Working Example Works on Android.工作示例适用于 Android。 Some bug in the web version.网络版中的一些错误。

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

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