简体   繁体   English

如何在react-native中更改默认的fontFamily

[英]How to change the default fontFamily in react-native

Can I set a default fontFamily in my app (iOS and Android)?? 我可以在我的应用程序(iOS和Android)中设置默认的fontFamily吗?

I don't want set it in each screen. 我不想在每个屏幕上进行设置。

Any idea? 任何想法?

The only way to do this is from the native side of things. 唯一的方法是从事物的本机方面。 See here: 看这里:

Set a default font for whole iOS app? 为整个iOS应用设置默认字体吗?

and

Is it possible to set a custom font for entire of application? 是否可以为整个应用程序设置自定义字体?

Although, if you use the library Cairn (disclaimer: I wrote it) it's trivial. 虽然,如果您使用凯恩图书馆(免责声明:我写过),那是微不足道的。 Simply define your global text style and extend from it everywhere. 只需定义您的全局文本样式并从任何地方扩展它即可。 Cairn makes all global stuff reusable while not breaking component-specific overrides and extensions. Cairn使所有全局内容都可重用,同时又不会破坏特定于组件的覆盖和扩展。

// styles.js
import { createStylesheet } from 'react-native';
import cairn from 'cairn';

export default cairn({
    text: {
        fontFamily: '...'
    }
}, (styles) => createStylesheet(styles));

Then extend those global styles in your component and apply to your text element: 然后在组件中扩展这些全局样式并将其应用于文本元素:

// components/MyComponent.js

import styleContext from '../styles';

const style = styleContext.extend({
    'text.header': {
        fontSize: 30
    }
});

export default const MyComponent = () => (
    // Spreads style={[styleContext.text, style['text.header']]}
    <Text {...style('text.header')}>Header!</Text>
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM