I'm having a long text in my app and I need to truncate it and add three dots to the end.
How can I do that in React Native Text element?
Thanks
This question is related to
react-native
ellipsis
To Achieve ellipses for the text use the Text property numberofLines={1} which will automatically truncate the text with an ellipsis you can specify the ellipsizeMode as "head", "middle", "tail" or "clip" By default it is tail
numberOfLines
parameter on a Text
component:<Text numberOfLines={1}>long long long long text<Text>
Will produce:
long long long…
(Assuming you have short width container.)
ellipsizeMode
parameter to move the ellipsis to the head
or middle
. tail
is the default value.<Text numberOfLines={1} ellipsizeMode='head'>long long long long text<Text>
Will produce:
…long long text
NOTE: The Text
component should also include style={{ flex: 1 }}
when the ellipsis needs to be applied relative to the size of its container. Useful for row layouts, etc.
const styles = theme => ({_x000D_
contentClass:{_x000D_
overflow: 'hidden',_x000D_
textOverflow: 'ellipsis',_x000D_
display: '-webkit-box',_x000D_
WebkitLineClamp:1,_x000D_
WebkitBoxOrient:'vertical'_x000D_
} _x000D_
})
_x000D_
render () {_x000D_
return(_x000D_
<div className={classes.contentClass}>_x000D_
{'content'}_x000D_
</div>_x000D_
)_x000D_
}
_x000D_
You can use ellipsizeMode and numberOfLines. e.g
<Text ellipsizeMode='tail' numberOfLines={2}>
This very long text should be truncated with dots in the beginning.
</Text>
<Text ellipsizeMode='tail' numberOfLines={2} style={{width:100}}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at cursus
</Text>
Result: Lorem ipsum...
<View
style={{
flexDirection: 'row',
padding: 10,
}}
>
<Text numberOfLines={5} style={{flex:1}}>
This is a very long text that will overflow on a small device This is a very
long text that will overflow on a small deviceThis is a very long text that
will overflow on a small deviceThis is a very long text that will overflow
on a small device
</Text>
</View>
Source: Stackoverflow.com