简体   繁体   中英

Font is not loading on either IOS or Android

在此处输入图片说明 It says I can use Expo.Apploading but I have no idea about that. If someone could help it would be great. This happens in either of device I am running. Below is code for App.js

import React, {useState}from 'react';
import {Text, View } from 'react-native';
import *as Font from 'expo-font';
import {AppLoading} from 'expo';

import {enableScreens} from 'react-native-screens';
import EstesGuideNavigator from './navigation/EstesGuideNavigator'; 

enableScreens(); // useScreens has been changed to enableScreens - this helps screens load faster in background

const fetchFonts = () => {   //fetching costum fonts for my app using Async
  Font.loadAsync({
    'raleway-blackItalic' : require('./assets/fonts/Raleway-BlackItalic.ttf'),
    'raleway-bold' : require('./assets/fonts/Raleway-Bold.ttf'),
    'raleway-regular' : require('./assets/fonts/Raleway-Regular.ttf'),
    'raleway-thin' : require('./assets/fonts/Raleway-Thin.ttf')
  });
}; 

export default function App()  {

const [fontLoaded, setFontLoaded] = useState(false); //initially it's false because app hasn't been loaded 

if (!fontLoaded) {
  return(
  <AppLoading 
  startAsync = {fetchFonts} 
  onFinish = {() => setFontLoaded(true) }
  /> //if assets(fonts here) is not loaded we display loading screen and load assets for app
  );  

} 
return <EstesGuideNavigator/>;

}

Is there any way to resolve this? 在此处输入图片说明

I don't know what's wrong but this is how i handle my font loading, Works perfectly

import { AppLoading } from "expo";
import { Asset } from "expo-asset";
import * as Font from "expo-font";
import React, { useState } from "react";
import { Platform, StatusBar, StyleSheet, View } from "react-native";
import { Ionicons } from "@expo/vector-icons";

import AppNavigator from "./navigation/AppNavigator";

export default function App(props) {
  const [isLoadingComplete, setLoadingComplete] = useState(false);

  if (!isLoadingComplete && !props.skipLoadingScreen) {
    return (
      <AppLoading
        startAsync={loadResourcesAsync}
        onError={handleLoadingError}
        onFinish={() => handleFinishLoading(setLoadingComplete)}
      />
    );
  } else {
    return (
      <View style={styles.container}>
        {Platform.OS === "ios" && <StatusBar barStyle="default" />}
        <AppNavigator />
      </View>
    );
  }
}

async function loadResourcesAsync() {
  await Promise.all([
    Asset.loadAsync([
      require("./assets/images/tick.png"),
      require("./assets/images/error.png")
    ]),
    Font.loadAsync({
      // This is the font that we are using for our tab bar
      ...Ionicons.font,
      // We include SpaceMono because we use it in HomeScreen.js. Feel free to
      // remove this if you are not using it in your app
      "space-mono": require("./assets/fonts/SpaceMono-Regular.ttf"),
      "Gilroy-Bold": require("./assets/fonts/Gilroy-Bold.otf"),
      "Gilroy-Medium": require("./assets/fonts/Gilroy-Medium.otf"),
      "Gilroy-Heavy": require("./assets/fonts/Gilroy-Heavy.otf")
    })
  ]);
}

function handleLoadingError(error) {
  // In this case, you might want to report the error to your error reporting
  // service, for example Sentry
  console.warn(error);
}

function handleFinishLoading(setLoadingComplete) {
  setLoadingComplete(true);
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff"
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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