react-native-global-props icon indicating copy to clipboard operation
react-native-global-props copied to clipboard

fontFamily cannot be overriden

Open thomasttvo opened this issue 6 years ago • 15 comments

const customTextProps = {
  style: {
    fontFamily: 'Nunito Sans'
  }
};

setCustomText(customTextProps);

When using the Inspector, the Text component's fontFamily still shows up as System. Apparently, react-native automatically prepended {fontFamily: 'System', fontSize:16, color:'#000'} to Text's style prop, which overrides our own custom style prop.

thomasttvo avatar May 01 '18 08:05 thomasttvo

@thomasttvo Are you passing an array of style to Text component?

mahesh1996 avatar May 01 '18 08:05 mahesh1996

@mahesh1996 this issue, however, happens even when I'm not using an array

thomasttvo avatar May 01 '18 08:05 thomasttvo

@thomasttvo I'm looking into it.

mahesh1996 avatar May 01 '18 08:05 mahesh1996

@thomasttvo It's working fine. Have you added rnmp in package.json to tell RN where your font is located?

mahesh1996 avatar May 01 '18 10:05 mahesh1996

@mahesh1996 yes, my font works in general, just not with global props. It works when I add fontFamily as inline style or in Stylesheet, but when I set it with global-props, the fontFamily becomes System

thomasttvo avatar May 01 '18 10:05 thomasttvo

Did we never find a solution to this?

flikQ avatar Jul 19 '18 19:07 flikQ

Work around:

let sourceRender = (Text as any).render;
Text.render = function render(props: any, ref: any) {
   return sourceRender.apply(this, [{ ...props, style: patchStyles(props.style) }, ref]);
};

ex3ndr avatar Jul 27 '18 16:07 ex3ndr

@ex3ndr thanks, however I think you might need to elaborate on this some what. Seems a little vague.

flikQ avatar Aug 07 '18 13:08 flikQ

No elaboration @ex3ndr? It would be helpful if you could give a little more info of the context and placement of this snippet. @mahesh1996 any ideas? Or maybe has this library gone stale?

flikQ avatar Feb 20 '19 11:02 flikQ

You can place above snippet given by @ex3ndr in index.js file of your project. It will modify render method of Text component. But I couldn't get where is patchStyles function defined.

mahesh1996 avatar Feb 20 '19 11:02 mahesh1996

Still nothing @ex3ndr ?

Going to ditch this library and try something else.

flikQ avatar Apr 23 '19 19:04 flikQ

Here's a version of the code from @ex3ndr that works in 2019

const sourceRender = Text.render;
Text.render = function render(props: any, ref: any) {
  return sourceRender.apply(this, [{ ...props, style: [{ fontFamily: 'CourierNewPSMT' }, props.style] }, ref]);
};

anshul-kai avatar Jun 07 '19 22:06 anshul-kai

Tested react-native-global-props 1.1.5 with react-native 0.59.9 and it seems to be working. Are you guys still seeing this issue? If so, can someone give me a repro?

Ajackster avatar Jun 12 '19 12:06 Ajackster

It does not work with current react-native: 0.62.2

pke avatar May 20 '20 14:05 pke

It does not work with current react-native: 0.62.2 try this - https://www.npmjs.com/package/react-native-simple-default-props

dioi2000 avatar Jan 20 '22 01:01 dioi2000