React Native开发中如何为IOS引入字体

376 阅读1分钟

打开Xcode,选择Open a project or file

image.png

找到RN工程下的ios下的.xcodeproj文件并打开

image.png

打开之后,界面像下面这样

image.png

点击右键,在菜单中选择New Group,起名Resources

image.png

选择Resources,右键菜单中选择Add File to “xxx”

image.png

找到RN项目下,node_modules下,你要引用的字体,选择字体所在的文件夹,点击添加

image.png

xCode界面上,会看到刚添加的字体们

image.png

确认Build Phases下的Copy Bundle Resources下有那些字体

image.png

打开RN工程下的ios下项目名下的info.plist,粘贴字体名字

我引用的字体名字如下,每个人情况不一样,但是格式都是一样的

<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>
  <string>Fontisto.ttf</string>
</array>

image.png

回到xCode,确认Fonts provided by application下是否有你加的这些字体。

image.png

在RN工程根目录下,新建react-native.config.js,粘贴如下配置

module.exports = {
  dependencies: {
    'react-native-vector-icons': {
      platforms: {
        ios: null,
      },
    },
  },
};

记得保存文件

image.png

RN工程下ios下执行这几步,然后字体就好用了

  1. 进入ios文件夹
  2. run pod install
  3. run rm -rf build
  4. run cd ..
  5. run yarn ios