首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >been本机/世博/ NativeBase - fontFamily "Roboto_medium“不是系统字体,也没有通过Font.loadAsync加载

been本机/世博/ NativeBase - fontFamily "Roboto_medium“不是系统字体,也没有通过Font.loadAsync加载
EN

Stack Overflow用户
提问于 2021-03-24 00:01:22
回答 1查看 552关注 0票数 1

我正在创建一个移动应用程序使用博览,并希望使用NativeBase的UI组件。

不管我想做什么,我都会遇到这个恼人的错误:fontFamily "Roboto_medium" is not a system font and has not been loaded through Font.loadAsync

我看了一下文档,并从那里使用了这个例子,它起作用了!

代码语言:javascript
复制
import React from 'react';
import { AppLoading } from 'expo';
import { Container, Text } from 'native-base';
import * as Font from 'expo-font';
import { Ionicons } from '@expo/vector-icons';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isReady: false,
    };
  }

  async componentDidMount() {
    await Font.loadAsync({
      Roboto: require('native-base/Fonts/Roboto.ttf'),
      Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf'),
      ...Ionicons.font,
    });
    this.setState({ isReady: true });
  }

  render() {
    if (!this.state.isReady) {
      return <AppLoading />;
    }

    return (
      <Container>
        <Text>Open up App.js to start working on your app!</Text>
      </Container>
    );
  }
}

请注意它们如何在componentDidMount()中加载字体

现在,你可以看到,这是旧的反应,我想使用钩子和函数组件。

我试过这个:

代码语言:javascript
复制
  useEffect(() => {
    (async () => await Font.loadAsync({
      Roboto: require('native-base/Fonts/Roboto.ttf'),
      Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf'),
    }))();
     }, [])

我试过了:

代码语言:javascript
复制
  useEffect(async() => {
      await Font.loadAsync({
        Roboto: require('native-base/Fonts/Roboto.ttf'),
        Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf'),
        ...Ionicons.font,
      });
  }, [])

对我来说什么都不管用!有人能帮忙吗?我怎样才能加载这些字体?

EN

回答 1

Stack Overflow用户

发布于 2022-04-19 00:28:21

首先,我们必须安装我们想要使用的世博google字体,例如:

代码语言:javascript
复制
expo install expo-font @expo-google-fonts/inter expo-app-loading

下一步,我们为本机基础提供程序创建一个主题文件theme.js,请参阅参考资料:https://docs.nativebase.io/next/customizing-fonts

代码语言:javascript
复制
import { extendTheme } from "native-base";

export const theme = extendTheme({
  fontConfig: {
    Inter: {
      400: {
        normal: "Inter_400Regular",
      },
      500: {
        normal: "Inter_500Medium",
      },
      600: {
        normal: "Inter_600SemiBold",
      },
    },
  },
  fonts: {
    heading: "Inter",
    body: "Inter",
    mono: "Inter",
  },
});

最后,我们链接主题文件并将字体加载到App,参见参考:https://docs.expo.dev/guides/using-custom-fonts/

代码语言:javascript
复制
import {
  useFonts,
  Inter_400Regular,
  Inter_500Medium,
  Inter_600SemiBold
} from "@expo-google-fonts/inter";
import AppLoading from 'expo-app-loading';
import { NativeBaseProvider, Box, Text } from "native-base";
import { theme } from "./theme";

export default function App() {
  let [fontsLoaded] = useFonts({
    Inter_400Regular,
    Inter_500Medium,
    Inter_600SemiBold
  });

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

  return (
    <NativeBaseProvider theme={theme}>
      <Box flex={1} bg="#fff" alignItems="center" justifyContent="center">
        <Text fontWeight="500">
          Expo Google Font with Native Base
        </Text>
      </Box>
    </NativeBaseProvider>
  );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66772944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档