1

I am unable to center a Text component in a View component in React Native both on android as well as ios.

As you can see the + sign in not centred in the white circle.

enter image description here

This is my component:


<TouchableOpacity
    style={styles.blueButton}
>
    <View style={styles.addButton}>
        <Text style={styles.plus}>+</Text>
    </View>
</TouchableOpacity>

This is my stylesheet:

blueButton: {
    height: 40,
    width: 40,
    borderRadius: 3,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: '#3498DB',
},
addButton: {
    width: 15,
    height: 15,
    borderRadius: 30,
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'white',
},
plus: {
    color: '#3498DB',
    fontSize: 20,
},
Vinay Sharma
  • 3,291
  • 4
  • 30
  • 66

2 Answers2

0

Can you try my below code:

Compnent code:

<View style={styles.addButtonBlue}>
  <View style={styles.addButton}>
    <Text style={styles.plus}>+</Text>
  </View>
</View>

Stylesheet Code

addButtonBlue: {
  width: 70,
  height: 70,
  borderRadius: 10,
  justifyContent: 'center',
  alignContent: 'center',
  alignItems: 'center',
  backgroundColor: '#3498db',
},
addButton: {
  width: 30,
  height: 30,
  borderRadius: 30,
  justifyContent: 'center',
  alignContent: 'center',
  alignItems: 'center',
  backgroundColor: '#fff',
},
plus: {
    color: '#3498DB',
},

Screenshot

Ajith
  • 2,476
  • 2
  • 17
  • 38
0

The extra font padding in android might be the culprit here. Try adding includeFontPadding: false to the text style.

more info here - https://facebook.github.io/react-native/docs/text#style

includeFontPadding: bool (Android)

Set to false to remove extra font padding intended to make space for certain ascenders / descenders. With some fonts, this padding can make text look slightly misaligned when centered vertically. For best results also set textAlignVertical to center. Default is true.

Community
  • 1
  • 1
LonelyCpp
  • 2,533
  • 1
  • 17
  • 36