Font.loadAsync()无法解析自定义字体模块(React Native& Expo)

时间:2017-04-29 12:17:22

标签: reactjs react-native expo

我的应用程序使用CRNA和Expo,我的问题是Font.loadAsync()异步函数无法在项目目录的assets / fonts /文件夹中找到.otf字体文件。我绝对相信目录和文件名是正确的。我收到此错误。

link to image of my error screen

这是我的代码:

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Font, AppLoading } from 'expo'
import Root from './js/Root';

export default class App extends Component {

  constructor(props) {
    super(props)
    this.state = {
      fontLoaded: false
    }
  }

  async componentDidMount() {
    await Font.loadAsync({
      "Light": require('./assets/fonts/SemplicitaPro-Light.otf')
    })
    this.setState({ fontLoaded: true })
  }

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

这是我的package.json:

    {
  "name": "Zumer",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "flow-bin": "^0.40.0",
    "jest-expo": "^0.4.0",
    "react-native-scripts": "0.0.28",
    "react-test-renderer": "16.0.0-alpha.6"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "node node_modules/jest/bin/jest.js --watch",
    "flow": "flow"
  },
  "packagerOpts": {
    "assetExts": ["ttf", "otf"]
  },
  "jest": {
    "preset": "jest-expo",
    "tranformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation)"
    ]
  },
  "dependencies": {
    "@expo/vector-icons": "^4.0.0",
    "expo": "^16.0.0",
    "native-base": "^2.1.2",
    "react": "16.0.0-alpha.6",
    "react-native": "^0.43.4",
    "react-native-elements": "^0.11.1",
    "react-navigation": "^1.0.0-beta.8",
    "react-redux": "^5.0.4",
    "redux": "^3.6.0",
    "redux-observable": "^0.14.1",
    "redux-persist": "^4.6.0",
    "rxjs": "^5.3.0"
  }
}

世博会是否支持.otf文件格式?

2 个答案:

答案 0 :(得分:3)

我通过将otf文件转换为ttf

来解决此问题

答案 1 :(得分:2)

我有一个类似的问题,我可以通过删除需要字体源的密钥周围的引号来解决它。

在:

await Font.loadAsync({
  "Light": require('./assets/fonts/SemplicitaPro-Light.otf')
})

await Font.loadAsync({
  Light: require('./assets/fonts/SemplicitaPro-Light.otf')
})

并在样式表中使用它,如此

const styles = StyleSheet.create({
   text: {
      fontFamily: 'Light'
   }
});

之后一切正常。