How to set default font family in React Native?

后端 未结 10 1007
鱼传尺愫
鱼传尺愫 2020-11-29 00:25

Is there an equivalent to this CSS in React Native, so that the app uses the same font everywhere ?

body {
  font-family: \'Open Sans\';
}

10条回答
  •  爱一瞬间的悲伤
    2020-11-29 00:56

    Add this function to your root App component and then run it from your constructor after adding your font using these instructions. https://medium.com/react-native-training/react-native-custom-fonts-ccc9aacf9e5e

    import {Text, TextInput} from 'react-native'
    
    SetDefaultFontFamily = () => {
        let components = [Text, TextInput]
    
        const customProps = {
            style: {
                fontFamily: "Rubik"
            }
        }
    
        for(let i = 0; i < components.length; i++) {
            const TextRender = components[i].prototype.render;
            const initialDefaultProps = components[i].prototype.constructor.defaultProps;
            components[i].prototype.constructor.defaultProps = {
                ...initialDefaultProps,
                ...customProps,
            }
            components[i].prototype.render = function render() {
                let oldProps = this.props;
                this.props = { ...this.props, style: [customProps.style, this.props.style] };
                try {
                    return TextRender.apply(this, arguments);
                } finally {
                    this.props = oldProps;
                }
            };
        }
    }
    

提交回复
热议问题