我想在我的应用程序中使用来自谷歌字体的字体。 这是字体。
我已将 .ttf 文件放在 app/fonts
中。
包.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
。
然后在我的应用程序中使用字体:
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它有错误:
无法识别的字体系列“Orbitron”
我究竟做错了什么?
如何找出放置在 fontFamily: 'xxx'
中的确切值?
原文由 user2602079 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于版本 < 0.60 的 react-native 中 的自定义字体,这里有很多答案
对于那些使用 react-native version > 0.60 ,
'rnpm' is deprecated
和自定义字体的人将不起作用。现在,为了在 react-native 版本 > 0.60 中添加自定义字体,您必须:
1- 在项目的根文件夹中创建一个名为
react-native.config.js
的文件。2-将其添加到该新文件中
3- 在根项目路径中运行
react-native link
命令。4-使用
react-native run-android
或react-native run-ios
命令运行它更新步骤 3
对于那些在react-native 版本 >= 0.69.x上运行的用户,由于
link
已被弃用,因此npx react-native-asset
react-native link
将不再起作用,命令react-native link
将被替换---
。有关该版本的更多信息,请参见此处: https ://github.com/react-native-community/cli/releases/tag/v8.0.0