tldr: set alignItems
to any value other than 'stretch'
for the style of the parent View of your View containing Text
The issue your facing is likely related to React Native's default value for alignItems: 'stretch'
on a <View />
element. Basically, all <View />
elements by default cause their children to stretch along the cross axis (axis opposite to the flexDirection
). Setting alignItems
to any value other than "stretch"
("baseline", "flex-start", "flex-end", or "center") on the parent View prevents this behavior and addresses your problem.
Below is an example where there are two View elements contained inside of a parent View with a blue border. The two View elements each contain a View wrapped around a Text element. In the case of the first View with default styling, the yellow child View expands horizontally to fill the entire width. In the second View where alignItems: 'baseline'
, the pink View does not expand and stays the size of it's child Text element.
<View style={{ borderWidth: 5, borderColor: 'blue' }}>
<View>
<View style={{ backgroundColor: 'yellow' }}>
<Text style={{ fontSize: 30 }}>Hello</Text>
</View>
</View>
<View style={{ alignItems: 'baseline' }}>
<View style={{ backgroundColor: 'pink' }}>
<Text style={{ fontSize: 30 }}>Hello</Text>
</View>
</View>
</View>
The align-items
property is explained well here.