反应本机不接受用Usefonts钩进口的字体

发布于 2025-02-02 02:36:08 字数 496 浏览 3 评论 0原文

我将一些带有Expo cli的Usefonts导入了一些自定义字体文件,但是有一个错误的说法:

fontfamily“ Productsans”不是系统字体,尚未通过字体加载。loadasync

这是代码的

import { useFonts } from 'expo-font';

const FontScreen= () => {

  const [customFonts] = useFonts({
    ProductSans: '../../assets/fonts/ProductSansRegular.ttf',
    ProductSansBold: '../../assets/fonts/ProductSansBold.ttf',
  });

<Text style={{fontFamily: 'ProductSans' }}>Text</Text>
}

问题是什么?

I imported some custom font files with useFonts with expo cli, but there's an error saying,

fontFamily "ProductSans" is not a system font and has not been loaded through Font.loadAsync

Here's the code

import { useFonts } from 'expo-font';

const FontScreen= () => {

  const [customFonts] = useFonts({
    ProductSans: '../../assets/fonts/ProductSansRegular.ttf',
    ProductSansBold: '../../assets/fonts/ProductSansBold.ttf',
  });

<Text style={{fontFamily: 'ProductSans' }}>Text</Text>
}

What is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弱骨蛰伏 2025-02-09 02:36:08

usefonts()挂钩工作类似:

useFonts(map: string | {
    [fontFamily: string]: FontSource;
}): [boolean, Error | null]

Load a map of custom fonts to use in textual elements. 
The map keys are used as font names, and can be used with fontFamily: <name>;. 
It returns a boolean describing if all fonts are loaded.

Note, the fonts are not "reloaded" when you dynamically change the font map.

由于customfonts是一个布尔值,它描述了字体是否加载,因此仅在custic> custicfonts 是正确的。另外,请确保使用需要函数来正确加载它们:

import { useFonts } from 'expo-font';

const FontScreen = () => {

  const [fontsLoaded] = useFonts({
    ProductSans: require('../../assets/fonts/ProductSansRegular.ttf'),
    ProductSansBold: require('../../assets/fonts/ProductSansBold.ttf'),
  });

 return (
    <>
        {fontsLoaded && <Text style={{fontFamily: 'ProductSans'}}>
            Text
         </Text>
    </>
}

时将功能正确加载:

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

const FontScreen = () => {

  const [fontsLoaded] = useFonts({
    ProductSans: require('../../assets/fonts/ProductSansRegular.ttf'),
    ProductSansBold: require('../../assets/fonts/ProductSansBold.ttf'),
  });

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

 return (
    <Text style={{fontFamily: 'ProductSans'}}>
            Text
    </Text>
}

您还可以在fontsloaded是false:更多信息在这里

The useFonts() hook works like so:

useFonts(map: string | {
    [fontFamily: string]: FontSource;
}): [boolean, Error | null]

Load a map of custom fonts to use in textual elements. 
The map keys are used as font names, and can be used with fontFamily: <name>;. 
It returns a boolean describing if all fonts are loaded.

Note, the fonts are not "reloaded" when you dynamically change the font map.

Since the customFonts is a boolean that describes if the fonts are loaded, you should only apply the font if customFonts is true. Also, make sure to surround your font paths with the require function to load them properly:

import { useFonts } from 'expo-font';

const FontScreen = () => {

  const [fontsLoaded] = useFonts({
    ProductSans: require('../../assets/fonts/ProductSansRegular.ttf'),
    ProductSansBold: require('../../assets/fonts/ProductSansBold.ttf'),
  });

 return (
    <>
        {fontsLoaded && <Text style={{fontFamily: 'ProductSans'}}>
            Text
         </Text>
    </>
}

You could also have the expo loading screen show while fontsLoaded is false:

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

const FontScreen = () => {

  const [fontsLoaded] = useFonts({
    ProductSans: require('../../assets/fonts/ProductSansRegular.ttf'),
    ProductSansBold: require('../../assets/fonts/ProductSansBold.ttf'),
  });

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

 return (
    <Text style={{fontFamily: 'ProductSans'}}>
            Text
    </Text>
}

More info here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文