React Native 上无法识别的字体系列

IT技术 javascript ios reactjs fonts react-native
2021-05-18 19:36:42

我在使用 React Native 时遇到了一个特殊的错误。在我里面我button.js在做

import Icon from "react-native-vector-icons/MaterialIcons";
const icon = (Icon name="menu size={20} color="green"/>);
render()
    return(
         {icon}
    )

但我得到了错误

Unrecognized Font Family 'Material Icons'

但是,当我将 FontAwesome 导入为:

import Icon from "react-native-vector-icons/FontAwesome";

我没有错误。

4个回答

触发loadFont()方法后修复了相同的问题

import Icon from 'react-native-vector-icons/FontAwesome';

Icon.loadFont();

确保您已运行以下命令:

react-native 链接 react-native-vector-icons

npm install --save react-native-vector-icons

将下面的行添加到 ios/podfile 文件然后运行 pod update

pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons'

在 ios/projectNameFolder/info.plist 中,在标签之间添加以下代码。如果您已经有了这个 UIAppFonts 数组,只需编辑字符串。确保你在 node_modules>react-native-vector-icons 下有字体

<key>UIAppFonts</key>
<array>
  <string>AntDesign.ttf</string>
  <string>Entypo.ttf</string>
  <string>EvilIcons.ttf</string>
  <string>Feather.ttf</string>
  <string>FontAwesome.ttf</string>
  <string>FontAwesome5_Brands.ttf</string>
  <string>FontAwesome5_Regular.ttf</string>
  <string>FontAwesome5_Solid.ttf</string>
  <string>Foundation.ttf</string>
  <string>Ionicons.ttf</string>
  <string>MaterialIcons.ttf</string>
  <string>MaterialCommunityIcons.ttf</string>
  <string>SimpleLineIcons.ttf</string>
  <string>Octicons.ttf</string>
  <string>Zocial.ttf</string>
</array>

清理缓存并通过重建重新启动应用程序。它假设工作。

您使用的库有一个关于此问题的特定部分,它指出(在 2.0.3 版中):

  • 确保您已将字体添加到 XCode 项目中。
  • 检查您尝试使用的字体是否出现在 Info.plist 中,如果您添加了整个文件夹并且它是蓝色的,那么您需要将其添加到路径中。
  • 检查字体是否在构建阶段的复制捆绑资源中复制。
  • 重新编译项目。