Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button text not aligned center vertically in react native

I am facing one issue with aligning text vertically center for button but it remains slightly lower then exact center. I found includeFontPadding from documentation with suggesting some discrepancies with some third party font.

Font looks proper in iOS devices but it is not properly centered with Android.

https://facebook.github.io/react-native/docs/text-style-props#includefontpadding

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.

<Button
    style={[style.button]} block >
     <Text>Setup Now</Text>
  </Button>

Style for button:

export default {
  button: {
    elevation: null,
    shadowColor: null,
    shadowOffset: null,
    shadowOpacity: null,
    shadowRadius: null
  }
}

enter image description here

like image 690
technerd Avatar asked May 10 '19 06:05

technerd


2 Answers

Instead of using a Text component inside a Button Component. Use the prop "title" as defined in the Button Documentation:

https://facebook.github.io/react-native/docs/button.html

So your code will be

<Button
    style={[style.button]} title="Setup Now" block >
  </Button>
like image 194
Usman Kazmi Avatar answered Nov 14 '22 22:11

Usman Kazmi


If you are using react native default button then react native default button does not support style properties and also Instead of using a Text component inside a Button Component. Use the properties "title" as defined in the official Documentation for that please refer the below link

https://facebook.github.io/react-native/docs/button.html

Also react native provide various options for button for your customisation button and as per your use for that please refer the below link

https://facebook.github.io/react-native/docs/handling-touches

Please try this below solution, may help for your issue.

<TouchableOpacity
      activeOpacity={.5}
      style={styles.buttonStyle}>
      <Text style={styles.textStyle}>Setup Now</Text>
</TouchableOpacity>



buttonStyle: {
    padding: 14,
    backgroundColor: 'red',
    borderRadius: 6,
  },

  textStyle: {
    color: 'white',
    textAlign: 'center',
    fontWeight: 'bold',
    fontSize: 16,
  },
like image 25
SagarDihora Avatar answered Nov 14 '22 23:11

SagarDihora