A View lays out its children with Flexbox, as you know it from CSS. A Text doesn't, so the same two Text elements share a line when their parent is a Text and stack as separate blocks when it is a View. And with no limit set, a Text takes as many lines as its content needs.
Key terms
numberOfLines: aTextprop that caps how many lines the text can take and truncates the rest with an ellipsis.ellipsizeMode: aTextprop that sets where the text is cut oncenumberOfLinesis set.
Nested Text flows inline
Everything inside a Text uses text layout: nested elements are no longer rectangles, and they wrap when they reach the end of the line. Two nested Text elements sit inline, on a single line when there is room. Under a View, the same two each form their own block:
tsx<Text> <Text>Mash the bananas </Text> <Text>and add flour</Text> </Text> // |Mash the bananas and add flour| <View> <Text>Mash the bananas </Text> <Text>and add flour</Text> </View> // |Mash the bananas| // |and add flour |
On a line too short for both, the outer Text flows them as if they were one text, so the second part picks up right where the first one ends:
text|Mash the | |bananas and | |add flour |
Under the View, each part wraps inside its own block:
text|Mash the | |bananas | |and add flour|
Nested parts don't stack the way blocks do. To start one on a new line, put a literal newline before it, written {'\n'} in JSX:
tsx<Text style={{fontWeight: 'bold'}}>Chapter 3{'\n'}<Text style={{color: 'gray'}}>The long way home</Text></Text>
Limiting lines with numberOfLines
React Native applies numberOfLines after it lays out the text, line wrapping included, and truncates the text with an ellipsis so the total never exceeds that number. The default is 0, which means no limit. A negative value, or NaN, is replaced with 0, and a development build logs a console.error about it.
ellipsizeMode has to be set together with numberOfLines. It takes four values, shown here on the file name "recipe-card.pdf":
tail, the default, keeps the beginning and marks the missing end with an ellipsis, as in "recipe-ca...".headkeeps the end and puts the ellipsis at the start, as in "...e-card.pdf".middlekeeps the beginning and the end, with the ellipsis between them, as in "reci...d.pdf".clipdoesn't draw lines past the edge of the text container, and shows no ellipsis.
So a Text with numberOfLines and no ellipsizeMode ends in a trailing ellipsis. On Android, only tail works correctly when numberOfLines is greater than 1, so keep head, middle and clip for single-line text.
Put the limit on the outermost Text
numberOfLines and ellipsizeMode apply to the whole block of text, and that block belongs to the outermost Text, the one that isn't nested in another Text. A nested Text takes its own text styles, such as color and weight, and can have its own onPress, but it doesn't take paragraph props, so a line limit set on it isn't read. Set the limit on the outer element.
Inside RecipeCard
RecipeCard, the code example below the lesson, has three Text elements in a View. Its title line nests the cooking time inside a bold Text:
tsx<Text style={{fontWeight: 'bold'}}>Banana bread<Text style={{color: 'gray'}}> 45 min</Text></Text>
The time continues inline after the title, in gray and in bold. The description and the file name are siblings under the View, so each one starts its own block and inherits nothing from the title. Being direct children of the View also makes them outermost Text elements, so their limits are read. The description's numberOfLines={2} stops it at two lines and, with no ellipsizeMode, ends it in a trailing ellipsis. The file name keeps both ends:
tsx<Text numberOfLines={1} ellipsizeMode="middle">banana-bread-printable-recipe-card-large.pdf</Text>
When the name is too long for its line, middle keeps its start and its .pdf ending. It is a single line, so the Android limit on values above 1 doesn't apply.
Code Examples
import {Text, View} from 'react-native';
export default function RecipeCard() {
return (
<View>
<Text style={{fontWeight: 'bold'}}>Banana bread<Text style={{color: 'gray'}}> 45 min</Text></Text>
<Text numberOfLines={2}>Mash three ripe bananas, stir in melted butter, sugar, an egg and flour, then bake until a skewer comes out clean.</Text>
<Text numberOfLines={1} ellipsizeMode="middle">banana-bread-printable-recipe-card-large.pdf</Text>
</View>
);
}