【发布时间】:2021-06-27 09:35:06
【问题描述】:
我正在尝试为我的项目使用自定义字体。我已将“Roboto”的所有变体保存到我的资产文件夹中。
我的 react-native 配置
module.exports = {
project: {
ios: {},
android: {}
},
assets: ["./assets/fonts/"]
};
导出自定义字体的我的主题页面
import { Dimensions } from "react-native";
const { width, height } = Dimensions.get("window");
export const FONTS = {
largeTitle: { fontFamily: "Roboto-regular", fontSize: SIZES.largeTitle, lineHeight: 55 },
h1: { fontFamily: "Roboto-Black", fontSize: SIZES.h1, lineHeight: 36 },
h2: { fontFamily: "Roboto-Bold", fontSize: SIZES.h2, lineHeight: 30 },
}
const appTheme = { COLORS, SIZES, FONTS };
export default appTheme;
在我的主页上,我正在尝试使用该字体。
import React from "react"
import { SafeAreaView, StyleSheet, View, Text, TouchableOpacity, Image, FlatList } from "react-
native"
import { COLORS, SIZES, icons, images, FONTS } from '../constants'
function renderMainCategories(){
return (
<View style={{ padding:SIZES.padding * 2 }}>
<Text style={{ ...FONTS.h1 }}>Main</Text>
<Text style={{ ...FONTS.h1 }}>Categories</Text>
</View>
)}
正如您在 style={{ ...FONTS.h1 }} 中看到的那样,我觉得我做的一切都是正确的,但由于某种原因,它在尝试使用字体时给了我一个错误。
它给了我这个错误---
fontFamily "Roboto-Black" 不是系统字体,尚未通过 Font.loadAsync 加载。
我尝试找到 Font.loadAsync 并尝试将其添加到父组件,但它不起作用!我已经尝试过 npx react-native 链接!我真的很感谢有人提供建议,而不仅仅是页面链接,谢谢!
有什么建议吗?
【问题讨论】:
标签: javascript reactjs react-native fonts