Search code examples
react-nativefont-awesometext-styling

Wrap Entire React Native App With One Font


I am looking for a simple way of wrapping my entire react native app with one font but I am having trouble finding documentation on the best way to do this.

Does anyone have any suggestions or references I can read through? Mostly what I am reading is that I need to make a new Text file and then import the font for each text element which is not what I want to do.

References I have already read through: https://medium.com/@fasterpancakes/react-native-row-how-to-wrap-components-and-avoid-stylesheets-99406e068357 https://reactnative.dev/docs/text https://ospfolio.com/two-way-to-change-default-font-family-in-react-native/

I am looking to do something like this:

import {useFonts} from "@use-expo/font";
import {AppLoading} from "expo";
...
const App = createAppContainer(switchNavigator);

export default () => {
  const [fontsLoaded] = useFonts({
    "Raleway-Bold": require("./assets/fonts/Raleway-Bold.ttf"),
    "Raleway-Medium": require("./assets/fonts/Raleway-Medium.ttf"),
    "Raleway-Regular": require("./assets/fonts/Raleway-Regular.ttf"),
  });

  if (!fontsLoaded) {
    return <AppLoading />;
  }

  return (
    <AuthProvider>
      <App
        ref={(navigator) => {
          setNavigator(navigator);
        }}
        style={{fontFamily: "Raleway-Bold"}}
      />
    </AuthProvider>
  );
};

Solution

  • Could you just make a custom component with all the default styling you need and then use that everywhere instead of <Text>

    // DefaultText.jsx

    import React from "react";
    import { StyleSheet } from "react-native";
    
    const DefaultText = props => <Text style={styles.defaultText}>{props.children}</Text>;
    
    const styles = StyleSheet.create({
      defaultText: {
        fontFamily: "Raleway-Bold",
      },
    });
    
    export default DefaultText;
    

    // SomeComponent.jsx

    import React from "react";
    import DefaultText from "./DefaultText";
    
    const SomeComponent = props => (
      <View>
        <DefaultText>This should be in Raleway-Bold</DefaultText>
      </View>
    );
    
    export default SomeComponent;