How to change font family in React Navigation createStackNavigator
Asked Answered
C

4

19

I use React Navigation as the navigator component of my app. I want to change font family of the stack navigator. Currently I do this to change the font family in iOS and it works pretty well:

const LoggedInStack = createStackNavigator({
  Home: {
    screen: Home,
    navigationOptions: {
      title: 'payX',
      headerTitleStyle: {
        fontFamily: "my-custom-font"
      }
    }
  }
});

but it doesn't work in Android devices. How should I change the code to make it work also in Android?

Corinthians answered 4/9, 2018 at 6:36 Comment(2)
did you link your fonts correctly? and are you sure this font name is correct? (capital alphabet may affect results)Lemos
@AliSn yes, I used it in other part of project and it workedCorinthians
C
8

Thank you, i used it this way and it worked

const LoggedInStack = createStackNavigator({
   Home: { 
       screen: Home, 
       navigationOptions: { 
       headerTitle: <Text style={{ textAlign: 'center', flex: 1, fontFamily: 
        'my-custom-font'}}>payX</Text>  
       } 
   }
});
Corinthians answered 17/9, 2018 at 11:53 Comment(1)
It does work! At least for me, I had to send the color (which is no problem), but also the fontSize, which I found is different on Android and iOS.Occupier
S
31

Add fontWeight: "200" to headerTitleStyle and it will work.

const LoggedInStack = createStackNavigator({
  Home: {
    screen: Home,
    navigationOptions: {
      title: 'payX',
      headerTitleStyle: {
        fontFamily: "my-custom-font",
        fontWeight: "200"
      }
    }
  }
});

Default font weight is 500 and if it doesn't find a proper font with that weight, it will load default font instead.

Sisterly answered 15/9, 2018 at 11:36 Comment(0)
C
8

Thank you, i used it this way and it worked

const LoggedInStack = createStackNavigator({
   Home: { 
       screen: Home, 
       navigationOptions: { 
       headerTitle: <Text style={{ textAlign: 'center', flex: 1, fontFamily: 
        'my-custom-font'}}>payX</Text>  
       } 
   }
});
Corinthians answered 17/9, 2018 at 11:53 Comment(1)
It does work! At least for me, I had to send the color (which is no problem), but also the fontSize, which I found is different on Android and iOS.Occupier
C
2

I was commonly change the navigation header font with this code:

const stackScreen = {
  Home: {
    screen: HomeScreen,
    navigationOptions: {
      ....
      headerTitle: () => (
        <Text style={{ fontFamily: "ibarra-regular" }}>Home</Text>
      )
    }
  }
};

Note: the ibarra-regular is the sample custom font family that i was used. Enjoyed! i hope this work well for your code

Cinema answered 16/1, 2020 at 4:12 Comment(0)
L
1

rename your font in wherever it saved and link it properly then using react-native link and use correct name of your font and use it..

Lanfranc answered 15/9, 2018 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.