Reputation: 1667
As per this stack overflow solution custom font can be used by calling it in each and every Text
component. But we may happen to miss the same font family when we have some thousands of Text
components in our App. Then we lose consistency in case of font family.
Hence, Is there any way to use custom font family by not calling in every Text
component?
Upvotes: 6
Views: 9996
Reputation: 580
You can override Text behaviour by adding this in any of your component using Text:
Edit: Add this code in your App.js or main file
let oldRender = Text.render;
Text.render = function (...args) {
let origin = oldRender.call(this, ...args);
return React.cloneElement(origin, {
style: [{color: 'red', fontFamily: 'Arial'}, origin.props.style]
});
}
For react Native Version 0.56 or below, Add this code in your App.js or main file
let oldRender = Text.prototype.render;
Text.prototype.render = function (...args) {
let origin = oldRender.call(this, ...args);
return React.cloneElement(origin, {
style: [{color: 'red', fontFamily: 'Arial'}, origin.props.style]
});
};
Reference Or create your own component, such as MyAppText. MyAppText would be a simple component that renders a Text component using your universal style and can pass through other props, etc.
Upvotes: 1
Reputation: 774
Just go through the below link, I believe this your requirement
https://facebook.github.io/react-native/docs/text.html#limited-style-inheritance
How to set default font family in React Native?
Upvotes: 0