自定义字体在React Native中不起作用 [英] Custom font not working in React Native

查看:376
本文介绍了自定义字体在React Native中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中使用Google字体中的一种字体. 此处是字体.

I want to use a font from google fonts in my app. Here is the font.

我已将.ttf文件放置在app/fonts中.

I have placed the .ttf file in app/fonts.

package.json:

package.json:

{
    "name": "xxx",
    "version": "0.0.1",
    "private": true,
    "scripts": {
        "start": "node node_modules/react-native/local-cli/cli.js start",
        "test": "jest"
    },
    "rnpm": {
        "assets": ["./app/fonts"]
    },
    "jest": {
        "preset": "react-native",
        "moduleNameMapper": {
            "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
            "\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
        }
    },
    "dependencies": {
        "flow-typed": "^2.0.0",
        "immutable": "^3.8.1",
        "react": "~15.4.1",
        "react-native": "0.42.0",
        "react-native-vector-icons": "^4.0.0",
        "react-redux": "^5.0.3",
        "redux": "^3.6.0",
        "redux-immutable": "^4.0.0",
        "redux-observable": "^0.14.1",
        "rxjs": "^5.2.0"
    },
    "devDependencies": {
        "babel-eslint": "^7.1.1",
        "babel-jest": "19.0.0",
        "babel-preset-react-native": "1.9.1",
        "eslint": "^3.17.0",
        "eslint-plugin-flowtype": "^2.30.3",
        "eslint-plugin-jsx": "^0.0.2",
        "eslint-plugin-react": "^6.10.0",
        "eslint-plugin-react-native": "^2.3.1",
        "flow-bin": "^0.42.0",
        "jest": "19.0.2",
        "jest-cli": "^19.0.2",
        "react-test-renderer": "~15.4.1",
        "redux-devtools": "^3.3.2",
        "remote-redux-devtools": "^0.5.7"
    }
}

然后运行react-native link.

然后在我的应用中使用字体:

Then use the font in my app:

import { View, Text } from 'react-native'
import React from 'react'
import Width from '../width/Width'
import Shape from '../shape/Shape'
import Height from '../height/Height'
import Thickness from '../thickness/Thickness'

export const Volcalc = () => (
  <View style={styles.container}>
    <Text style={styles.text}>SHAPE</Text>
    <Shape />
    <Text style={styles.text}>HEIGHT</Text>
    <Height />
    <Text style={styles.text}>WIDTH</Text>
    <Width />
    <Text style={styles.text}>THICKNESS</Text>
    <Thickness />
  </View>
)

const $mainColor = '#00d1b2'
const styles = {
  container: {
    flex: 1,
    padding: 20,
    backgroundColor: $mainColor
  },
  text: {
    textAlign: 'center',
    color: 'rgba(255, 255, 255, 0.9)',
    fontSize: 15,
    fontFamily: 'Orbitron'
  }
}

在android中,它不显示新字体,但没有错误.在ios中有错误:

In android it doesn't show the new font but has no error. In ios it has error:

无法识别的字体家族"Orbitron"

Unrecognised font family "Orbitron"

我在做什么错了?

如何找到要放置在fontFamily: 'xxx'中的精确值?

How do I find out the EXACT value to place in fontFamily: 'xxx'?

推荐答案

对于 react-native版本< 0.60

对于使用本机版本> 0.60 'rnpm' is deprecated 和自定义字体的用户将无效.

For those who are using react-native version > 0.60 , 'rnpm' is deprecated and custom fonts will not work.

现在,为了在react-native版本> 0.60中添加自定义字体,您将必须:

Now, in order to add custom font in react-native version > 0.60 you will have to :

1-在项目的根文件夹中创建一个名为react-native.config.js的文件.

1- Create a file named react-native.config.js in the root folder of your project.

2-将此添加到该新文件中

2- add this in that new file

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

在根项目路径中

3-运行react-native link命令.

3- run react-native link command in the root project path.

4-使用react-native run-androidreact-native run-ios命令运行它

4- run it using react-native run-android or react-native run-ios command

这篇关于自定义字体在React Native中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆