【问题标题】:Custom font does not work on real android device after build as android自定义字体在构建为 android 后在真正的 android 设备上不起作用
【发布时间】:2019-10-09 08:38:29
【问题描述】:

我在 react-native expo 项目中使用了自定义字体。 它在带有 android 模拟器的 expo 平台上运行良好。 但是在我将项目构建为 APK 文件后,它并没有将所有文本显示为我的自定义字体。 我按照这样的教程(https://docs.expo.io/versions/latest/guides/using-custom-fonts/)进行操作,但它不起作用。 有什么可以在安卓上工作的解决方案吗?

我尝试使用这样的自定义字体。

class App extends Component {
  state = {
    fontLoaded: false
  };

  async componentDidMount() {
    await Font.loadAsync({
      "Antonio": require("./assets/fonts/Antonio-Light.ttf"),
      "Antonio-Bold": require("./assets/fonts/Antonio-Bold.ttf")
    });
    this.setState({ fontLoaded: true });
  }

  render() {
    const { fontLoaded } = this.state;
    if (!fontLoaded)
      return (
        <Text>Loading.....</Text>
      );
...........

这是我的 package.json 文件。

...


  "dependencies": {
    "@expo/vector-icons": "^9.0.0",
    "angle-normals": "^1.0.0",
    "art": "^0.10.1",
    "bunny": "^1.0.1",
    "expo": "^34.0.3",
    "expo-asset": "latest",
    "expo-asset-utils": "^1.1.1",
    "expo-font": "^6.0.1",
    "expo-gl": "~5.0.1",
    "expo-pixi": "^1.2.0",
    "expokit": "^32.0.3",
    "gl-mat4": "^1.1.4",
    "gl-react": "^3.13.0",
    "gl-react-expo": "^3.16.3",
    "lodash": "^4.17.11",
    "matter-js": "^0.13.0",
    "native-base": "^2.10.0",
    "prop-types": "^15.6.2",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-motion": "^0.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
    "react-native-animatable": "^1.2.2",
    "react-native-donkey-kong": "^0.5.1",
    "react-native-extended-stylesheet": "git+https://github.com/bberak/react-native-extended-stylesheet.git",
    "react-native-game-engine": "^0.10.1",
    "react-native-gesture-handler": "~1.2.1",
    "react-native-paper": "2.1.3",
    "react-native-responsive-screen": "^1.3.0",
    "react-native-svg": "^9.11.1",
    "react-native-web": "^0.11.2",
    "react-navigation": "^3.2.1",
    "react-navigation-redux-helpers": "^2.0.9",
    "react-redux": "^6.0.0",
    "react-style-proptype": "^3.2.2",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "regl": "^1.3.0"
  },

...

这是我的 app.json 文件。

{
  "expo": {
    "name": "Zenduja",
    "slug": "Zenduja-Pro",
    "privacy": "unlisted",
    "sdkVersion": "33.0.0",
    "platforms": [
      "ios",
      "android"
    ],
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/images/icon.png",
    "splash": {
      "image": "./assets/images/splash.png",
      "resizeMode": "cover",
      "backgroundColor": "#222222"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "assets/fonts/*"
    ],
    "ios": {
      "supportsTablet": true
    },
    "android": {
      "package": "com.aaa.aaa"
    },
    "packagerOpts": {
      "assetExts": [
        "ttf",
        "mp4",
        "otf",
        "xml"
      ]
    },
  }
}


请帮助我。 谢谢。

更新问题。

这是我的世博小吃加载字体项目。

https://snack.expo.io/@angelsinej/font-loading

【问题讨论】:

  • 世博小吃可以复制吗?
  • 感谢您的回复。正如我提到的,它在构建为 APK 文件后不起作用。我现在会在世博小吃中重现,完成后告诉你。
  • 但它在零食中起作用?
  • 签入 app.json:assetBundlePatterns":
  • 喜欢:“assetBundlePatterns”:[“assets/fonts/*.*”]

标签: react-native expo


【解决方案1】:

像这样添加到 app.json 中:

"assetBundlePatterns":["assets/fonts/*"]  

更新到 sdk 34 和相关的 react-native 包。

重新编译apk

【讨论】:

  • 我用 app.json 更新了我的问题。我添加了上面的这个部分,但在构建 apk 后我无法在 android 设备上看到更改的自定义字体。请检查我的 app.json 好吗?
  • 我用 * 语法更新了答案,请重建 apk。如果不起作用:将 apk 重命名为 zip 并查看文件是否存在。
  • 我重建了apk,但无法正常工作。我检查了我的 apk 作为一个 zip 文件,有两个名为 asset_2a8a6af373531a3500e240604935b8dc.ttfasset_975aacd71c6910b732c678b04b39ed86.ttf 的 TTF 字体文件。对吗?
  • 重新发布您的 app.json
  • 你可以试试 expo-fonts 7.0.0 包版本吗?
猜你喜欢
  • 2015-08-10
  • 2012-02-11
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
相关资源
最近更新 更多