4

I need to align two Text component with different fontSize in a row and vertically centered. I have following now https://snack.expo.io/@troublediehard/dmVuZ2

enter image description here

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.row}>
          <Text style={styles.text1}>Font size 20</Text>
          <Text style={styles.text2}>Font size 14</Text>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  row: {
    flexDirection: 'row',
    backgroundColor: 'red',
  },
  text1: {
    fontSize: 20,
    backgroundColor: 'blue',
  },
  text2: {
    fontSize: 14,
    backgroundColor: 'green',
  },
});
Georgi Hristozov
  • 3,899
  • 2
  • 17
  • 23
Dima Portenko
  • 3,443
  • 5
  • 35
  • 48
  • hey, ever found a solution to this? – Return-1 Feb 26 '19 at 10:30
  • @Return-1 check answer and comments below, there whole story. Looks like it was issue with custom font and stick with padding workaround. – Dima Portenko Feb 26 '19 at 10:33
  • hmm i wonder though if this solution would work uniformly to all devices. I too am using a custom ( albeit not rare ) font and it seems like the size of the Text is higher than the text itself which means that text doesnt align properly. – Return-1 Feb 26 '19 at 11:45

2 Answers2

7

For anyone that didn't found a solution yet, you have to wrap the text within another Text tag:

<View style={styles.container}>
  <View style={styles.row}>
    <Text>
      <Text style={styles.text1}>Font size 20</Text>
      <Text style={styles.text2}>Font size 14</Text>
    </Text>
  </View>
</View>
Ian Vasco
  • 1,280
  • 13
  • 17
4

You need to add alignItems: 'center' to styles.row

row: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: 'red',
},
Pritish Vaidya
  • 21,561
  • 3
  • 58
  • 76