MD Ashik
MD Ashik

Reputation: 9835

Default fonts for React native hole app BY calling one time fonts name

I want to use a Font for all place or full app.

I don't want to call it too much time.

It should be call one time SO is it Possible ??

AS Like:

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

I all ready get some Type Answer Like that's.. BY Calling Costume Components
and By calling Global Style like...

<Text style={{styles.text}}> Demo </ Text>

const style= { 
  style: { 
    fontFamily: yourFont
  }
}

But I don't want too use it like this way ..

Please give me any Update Answer or News...

Which help me to solve it BY Call one time..

NB: I all ready Complete my App 80%... It's have lot off View SO How can I get a Fonts everywhere by call one time anywhere.

Upvotes: 0

Views: 221

Answers (1)

NiFi
NiFi

Reputation: 2458

What I think you are seeking is not directly possible as it is antithetical to React principles of isolated components. IMO the best approach is to create a custom component and use it to "replace" the Text component in your app. It's even possible to use Text as name for that component, and just change imports accordingly.

export default class Text extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <Text style={styles.defaultText}>{this.props.children}</Text>
    );
  }
}

const styles = StyleSheet.create({
  defaultText: {
    fontFamily: 'System'
  }
});      

Then simply change imports in the components you want to use this font, from

import { Text } from 'react-native';

to

import Text from './Text.js';

Upvotes: 1

Related Questions