React Native. How to place two Text with different fontSize in row with vertical align center
Asked Answered
M

2

5

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',
  },
});
Moonshine answered 18/11, 2018 at 20:1 Comment(3)
hey, ever found a solution to this?Ripe
@Ripe check answer and comments below, there whole story. Looks like it was issue with custom font and stick with padding workaround.Moonshine
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.Ripe
C
5

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

row: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: 'red',
},
Crepuscule answered 18/11, 2018 at 20:54 Comment(5)
thanks, it works for my sample, but actually doesn't work in my project with custom fonts.Moonshine
It should vertically align the content, unless there is some other padding or margin involved.Crepuscule
works if I removing fontFamily from style, but not working with custom font.Moonshine
Not sure if it is the line-height issue with the font, try adjusting using lineHeightCrepuscule
I've tried, but not helped as well. I gave up on this, looks like some issue here, I simply added padding to center second font.Moonshine
E
10

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>
Emmert answered 13/6, 2019 at 15:58 Comment(2)
and set the Text wrapper to the smallest font sizePerspicacity
I used it, works fine 😎Mcandrew
C
5

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

row: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: 'red',
},
Crepuscule answered 18/11, 2018 at 20:54 Comment(5)
thanks, it works for my sample, but actually doesn't work in my project with custom fonts.Moonshine
It should vertically align the content, unless there is some other padding or margin involved.Crepuscule
works if I removing fontFamily from style, but not working with custom font.Moonshine
Not sure if it is the line-height issue with the font, try adjusting using lineHeightCrepuscule
I've tried, but not helped as well. I gave up on this, looks like some issue here, I simply added padding to center second font.Moonshine

© 2022 - 2024 — McMap. All rights reserved.