裸 React Native (React Native CLI) iOS AstroSpace 自定义字体未应用于 <Text>

Bare React Native (React Native CLI) iOS AstroSpace custom font not being applied into <Text>

提问人:Jorge Ortiz 提问时间:11/10/2023 更新时间:11/10/2023 访问量:22

问:

我正在尝试将自定义字体应用到我的裸 React Native 项目中到 iOS 平台,但它不适用。 以前我已经成功应用了从谷歌字体下载的蒙特塞拉特字体。

该字体称为 AstroSpace,并从此链接下载 https://www.fontspace.com/a-astro-space-font-f46028

这是我的react-native.config.js

module.exports = {
    project: {
        ios: {},
        android: {}
    },
    assets: ['./assets/fonts/'],
}

这是我package.json

{
  "name": "ProjectName",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest",
    "startMetro": "npx react-native start --reset-cache",
    "rn-packager": "npx react-native start --reset-cache",
    "rn-ios": "npx react-native run-ios"
  },
  "dependencies": {
    "@notifee/react-native": "^7.8.0",
    "@react-native-async-storage/async-storage": "^1.19.0",
    "@react-native-community/masked-view": "^0.1.11",
    "@react-native-community/netinfo": "^9.4.1",
    "@react-navigation/drawer": "^6.6.3",
    "@react-navigation/native": "^6.1.7",
    "@react-navigation/native-stack": "^6.9.13",
    "@rneui/base": "^4.0.0-rc.7",
    "@rneui/themed": "^4.0.0-rc.7",
    "axios": "^1.4.0",
    "buffer": "^6.0.3",
    "pod-install": "^0.1.38",
    "react": "18.2.0",
    "react-native": "0.72.0",
    "react-native-background-fetch": "^4.2.1",
    "react-native-document-picker": "^9.0.1",
    "react-native-file-viewer": "^2.1.5",
    "react-native-fs": "^2.20.0",
    "react-native-gesture-handler": "^2.12.0",
    "react-native-permissions": "^3.9.0",
    "react-native-reanimated": "^3.3.0",
    "react-native-safe-area-context": "^4.6.4",
    "react-native-screens": "^3.22.1",
    "react-native-toast-message": "^2.1.6",
    "react-native-vector-icons": "^9.2.0",
    "react-native-video": "^5.2.1",
    "rn-fetch-blob": "^0.12.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/eslint-config": "^0.72.2",
    "@react-native/metro-config": "^0.72.6",
    "@tsconfig/react-native": "^3.0.0",
    "@types/metro-config": "^0.76.3",
    "@types/react": "^18.0.24",
    "@types/react-native-video": "^5.0.15",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.2.1",
    "eslint": "^8.19.0",
    "jest": "^29.2.1",
    "metro-react-native-babel-preset": "0.76.5",
    "prettier": "^2.4.1",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  },
  "engines": {
    "node": ">=16"
  },
  "reactNativePermissionsIOS": [
    "MediaLibrary"
  ],
  "rnpm": {
    "assets": [
      "./assets/fonts/"
    ]
  }
}

这是我的字体项目文件夹结构

Font-Structure-Directory

我试过了:

  • 将 AstroSpace.ttf 移至 ./assets/fonts/*

  • npx react-native-asset

  • 跑去启动地铁npx react-native start --reset-cache

  • Ran 无法应用 astrospace 字体npx react-native run-ios

  • 还运行无法应用astrospace字体npx react-native run-ios --device "iPhone"

还尝试了以下操作:

  • 将.ttf重命名为 AstroSpace-Regular、Astrospace-Regular。

Code-Text-With-Font-Applied

我正在应用 AstroSpace => https://codeshare.io/AdjPox 的代码

这是结果

Font-Result-Element

css ios reactjs react-native 字体

评论


答:

0赞 Jorge Ortiz 11/10/2023 #1

解决了它,事实证明,在 mac 的字体簿中添加 AstroSpace.ttf 文件,然后我验证了它并弹出了这个窗口,并显示内部字体名称为 aAstroSpace,我应该像这样在我的组件中引用该名称

<文本样式={{ fontFamily: 'aAstroSpace', 颜色: '#d9d9d9', 字体大小:18 }}> 文本示例

AstroSpace Font Name

Text component AstroSpace