ReactNative: how to center text?

前端 未结 16 729
终归单人心
终归单人心 2020-12-22 18:40

How to center Text in ReactNative both in horizontal and vertical?

I have an example application in rnplay.org where justifyContent=\"center\" and

相关标签:
16条回答
  • 2020-12-22 19:21

    From headline' style remove height, justifyContent and alignItems. It will center the text vertically. Add textAlign: 'center' and it will center the text horizontally.

      headline: {
        textAlign: 'center', // <-- the magic
        fontWeight: 'bold',
        fontSize: 18,
        marginTop: 0,
        width: 200,
        backgroundColor: 'yellow',
      }
    
    0 讨论(0)
  • 2020-12-22 19:22
    textAlignVertical: "center"
    

    is the real magic.

    0 讨论(0)
  • Set these styles to image component: { textAlignVertical: "center", textAlign: "center" }

    0 讨论(0)
  • 2020-12-22 19:24

    In addition to the use cases mentioned in the other answers:

    To center text in the specific use case of a BottomTabNavigator, remember to set showIcon to false (even if you don't have icons in the TabNavigator). Otherwise the text will be pushed toward bottom of Tab.

    For example:

    const TabNavigator = createBottomTabNavigator({
      Home: HomeScreen,
      Settings: SettingsScreen
    }, {
      tabBarOptions: {
        activeTintColor: 'white',
        inactiveTintColor: 'black',
        showIcon: false, //do this
        labelStyle: {
          fontSize: 20,
          textAlign: 'center',
        },
        tabStyle: {
          backgroundColor: 'grey',
          marginTop: 0,
          textAlign: 'center',
          justifyContent: 'center',
          textAlignVertical: "center"
        }
      }
    
    0 讨论(0)
提交回复
热议问题