React-native: [Text] How to specify default font family?

Created on 11 Jan 2016  路  5Comments  路  Source: facebook/react-native

Is there any way to change default font family except change native code?
I don`t want to specify font family for every Text element,It would be a mess if I do so.

Locked

Most helpful comment

resolved.

let oldRender = Text.prototype.render;
Text.prototype.render = function (...args) {
    let origin = oldRender.call(this, ...args);
    return cloneElement(origin, {
        style: [origin.props.style, styles.defaultFontFamily]
    });
};

override Text.prototype.render works for me.
It would be much better if there is an official way to resolve this

All 5 comments

Hey magicismight, thanks for reporting this issue!

React Native, as you've probably heard, is getting really popular and truth is we're getting a bit overwhelmed by the activity surrounding it. There are just too many issues for us to manage properly.

  • If you don't know how to do something or something is not working as you expect but not sure it's a bug, please ask on StackOverflow with the tag react-native or for more real time interactions, ask on Discord in the #react-native channel.
  • If this is a feature request or a bug that you would like to be fixed, please report it on Product Pains. It has a ranking feature that lets us focus on the most important issues the community is experiencing.
  • We welcome clear issues and PRs that are ready for in-depth discussion. Please provide screenshots where appropriate and always mention the version of React Native you're using. Thank you for your contributions!

resolved.

let oldRender = Text.prototype.render;
Text.prototype.render = function (...args) {
    let origin = oldRender.call(this, ...args);
    return cloneElement(origin, {
        style: [origin.props.style, styles.defaultFontFamily]
    });
};

override Text.prototype.render works for me.
It would be much better if there is an official way to resolve this

this will affect custom icon when you use icon font

How to avoid this behavior then @pannz ?

@magicismight Thank you, your solution works. But wouldn't it be better to put styles.defaultFontFamily before origin.props.style so we can customize the default font attributes?

For example:

const styles = AppStyleSheet.create({
  defaultFontFamily: {
    fontFamily: "Proxima Nova",
    fontWeight: '300'
  },
  ...
  titleText: {
    fontWeight: 'bold' <- Customize the font weight.
  },
});
Was this page helpful?
0 / 5 - 0 ratings