简体   繁体   English

如何将“expo-splash-screen”与“expo-google-fonts”一起使用?

[英]How to use `expo-splash-screen` with `expo-google-fonts`?

The splash screen is using async operations to wait, while the fonts package is using a "custom hook" useFonts (I guess).初始屏幕使用异步操作等待,而字体包使用“自定义挂钩” useFonts (我猜)。 How to make the splash screen wait for the google fonts to load?如何让启动画面等待谷歌字体加载?

You can load fonts with loadAsync from expo-fonts , and manage splash screen with expo-splash-screen您可以从expo-fonts使用loadAsync加载字体,并使用expo-splash-screen管理启动画面

import * as SplashScreen from 'expo-splash-screen';
import * as Font from 'expo-font';
import { Inter_900Black } from '@expo-google-fonts/inter';

export default function App() {
  const [appIsReady, setAppIsReady] = useState(false);

  useEffect(() => {
    (async () => {
      try {
        await SplashScreen.preventAutoHideAsync();
        await Font.loadAsync({ Inter_900Black });
      }
      catch {
        // handle error
      }
      finally {
        setAppIsReady(true);
      }
    })();
  }, []);

  const onLayout = useCallback(() => {
    if (appIsReady) {
      SplashScreen.hideAsync();
    }
  }, [appIsReady]);

  if (!appIsReady) {
    return null;
  }

  return (
      <View style={styles.container} onLayout={onLayout}>
        <Text style={{fontFamily: 'Inter_900Black'}}>
          Example text
        </Text>
      </View>
  );
}

This is compete!这是竞争!

import React, { useCallback, useEffect, useState } from 'react';
import * as SplashScreen from 'expo-splash-screen';
import * as Font from 'expo-font';
import { Montserrat_400Regular, Montserrat_500Medium, Montserrat_700Bold, 
Montserrat_900Black } from '@expo-google-fonts/montserrat';

export default function App() {
const [appIsReady, setAppIsReady] = useState(false);

useEffect(() => {
 async function prepare() {
  try {
    // Keep the splash screen visible while we fetch resources
    await SplashScreen.preventAutoHideAsync();

    // Pre-load fonts, make any API calls you need to do here
    await Font.loadAsync({ Montserrat_900Black });
    
    // Artificially delay for two seconds to simulate a slow loading
    // experience. Please remove this if you copy and paste the code!
    await new Promise(resolve => setTimeout(resolve, 2000));
  } catch (e) {
    console.warn(e);
  } finally {
    // Tell the application to render
    setAppIsReady(true);
  }
}

prepare();
}, []);

const onLayoutRootView = useCallback(async () => {
 if (appIsReady) {
  // This tells the splash screen to hide immediately! If we call this after
  // `setAppIsReady`, then we may see a blank screen while the app is
  // loading its initial state and rendering its first pixels. So instead,
  // we hide the splash screen once we know the root view has already
  // performed layout.
  await SplashScreen.hideAsync();
}
}, [appIsReady]);

if (!appIsReady) {
 return null;
}

return (
 <View
  style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}
  onLayout={onLayoutRootView}>
  <Text style={{ fontFamily: 'Montserrat_900Black', fontSize: 18 }}>SplashScreen 
  Demo! 👋</Text>
</View>
);
}'

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

相关问题 如何解决 Warning: expo-app-loading is deprecated in favor of expo-splash-screen: use SplashScreen.preventAutoHideAsync() instead? - How solve Warning: expo-app-loading is deprecated in favor of expo-splash-screen: use SplashScreen.preventAutoHideAsync() instead? 如何摆脱控制台警告:不推荐使用 expo-app-loading 以支持 expo-splash-screen? - How to I get rid of Console Warning: expo-app-loading is deprecated in favour of expo-splash-screen? 您如何添加 expo-google-fonts 以便它们可以在没有权重的情况下与 fontFamily 一起使用? - How do you add expo-google-fonts so they can work with fontFamily without the weights? 错误:没有为给定视图 controller 注册本机启动画面。对于 react-native expo 使用 expo-splash-screen - Error: No native splash screen registered for given view controller. for react-native expo using expo-splash-screen 如何在Expo弹出屏幕上使用SVG图像 - how to use SVG Image in Splash Screen on Expo eject Expo 卡在启动画面上 - Expo Stuck on Splash Screen React Native &amp; Expo,如何控制闪屏? - React Native & Expo, how to control the splash screen? 如何让启动画面保持更长时间? (世博会cli) - How to keep splash screen longer on? (expo cli) 禁用默认 Expo 启动画面 - Disable default Expo splash screen Lottie Splash Screen with Expo - 带有 Expo 的动画启动画面 - Lottie Splash Screen with Expo - animated splash screen with Expo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM