Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show only the 1st line of text in React Native

How can I reveal the 1st line of text only? If I have something like this:

<View> 
  <Text>
    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, 
  </Text>
</View>

I only want "Contrary to popular belief, Lorem Ipsum" to show up.

like image 631
obumoon Avatar asked Oct 18 '18 01:10

obumoon


People also ask

How do I limit text length in react native?

Just use style={{flex: 1}} in card or text styling to limit within the card. Use numberOfLines={1} to restrict in one line.

How do I truncate text in react native?

As long as the container of the Text element has a Flex value (I use, 1), the text will be truncated within the container. ellipsizeMode='tail' is not needed as 'tail' is default value for ellipsizeMode. Unless you want to show ellipse in beginning of the text, you can use just numberOfLines prop and it should work.

How do I change the number of lines in text react native?

To determine number of lines of Text component with React Native, we can get it from the onTextLayout callback's parameter. to set numberOfLines to NUM_OF_LINES to restrict the number of lines to display. Then we set the onTextLayout prop to the onTextLayout function and get the number of lines displayed with e.

How do you break a line in text in react native?

You can use \n where you want to break the line.


1 Answers

You can use the prop numberOfLines to restrict the text from overflowing:

<Text numberOfLines={1}>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, 
</Text>

This will wrap your text when the limit of parent view is reached, like:

 Contrary to popular belief, Lorem Ipsum is not...
like image 173
Uzair A. Avatar answered Sep 27 '22 19:09

Uzair A.