有没有办法在React Native中全局设置字体?
问题描述:
我需要创建一种适用于整个应用程序中每个 Text
组件的自定义字体.
I need to create a custom font that applies to every Text
component in the whole application.
有没有办法在React Native中全局设置字体?
Is there is a way to set a font globally in React Native?
答
一种方法是为RN Text
说MyTextCustomFont创建包装器:
One way is to create a wrapper for RN Text
say MyTextCustomFont:
const MyTextCustomFont = (props) => {
return (
<Text style={{fontFamily:'myFont'}} {...props} >{props.children}</Text>
)
}
导入此 MyTextCustomFont
并在任何地方使用.
import this MyTextCustomFont
and use anywhere.
另一种方法是定义样式对象,然后在任何需要的地方使用它.
Another way is to define a style object and use it wherever you want.