Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-Native: Avoid Text Wrapping

I have the title of a song and its duration showing in one line. The song title needs to show an ellipsis but the duration should never wrap or show ellipsis. I've tried several combinations but fail to make this work right for long titles. The duration either goes off screen when the name shows ellipsis or the duration wraps. I can't hardcode a fixed width on the duration as it can change size.

<View style={{flexDirection: 'row'}}>     <Text numberOfLines={2} style={{fontSize: 16, textAlign: 'left'}}>{title}</Text>     <Text style={{flex: 1, fontSize: 13, textAlign: 'right', marginTop: 2}}>{duration}</Text> </View> 
like image 961
Anshul Koka Avatar asked Mar 28 '17 16:03

Anshul Koka


People also ask

How do you wrap Text in React Native Text?

To wrap React Native text on the screen, we can set flexWrap to 'wrap' .

How do you handle long texts in React Native?

just put text in a tag view and text will adapt to property of View. Not really, if you put a text of 22100 chars the whole component goes blank.

What is flexGrow in React Native?

flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its children, a container will distribute any remaining space according to the flex grow values specified by its children.

How do you Text ellipsis in React Native?

In React Native we can set the default Ellipsis using numberOfLines = { 1 } prop of Text component. This prop would allow us to implement the Ellipsis Clipped effect on Text.


2 Answers

The solution ended up being fairly simple. Not entirely intuitive but here's how to solve this. It appears that the text that needs ellipsis requires flex: 1.

 <View style={{ flexDirection: "row" }}> <Text numberOfLines={1} style={{ flex: 1, textAlign: "left" }}>     {title} </Text> <Text style={{ textAlign: "right" }}>{duration}</Text> </View>; 
like image 80
Anshul Koka Avatar answered Oct 04 '22 07:10

Anshul Koka


Possibly below solution should satify your creteria

 return (         <View style={{flexDirection: 'row', flex: 1, justifyContent: 'space-around', marginTop: 50}}>             <Text numberOfLines={2} style={{fontSize: 16, flex: 1}}>{title}</Text>             <Text style={{fontSize: 13, marginTop: 2}}>{duration}</Text>         </View>     ); 

Please check and let me know if does not work.

like image 31
Durgaprasad Budhwani Avatar answered Oct 04 '22 07:10

Durgaprasad Budhwani