Answers:
使用numberOfLines
https://rnplay.org/plays/ImmKkA/edit
或者,如果您知道/或可以计算每行的最大字符数,则可以使用JS子字符串。
<Text>{ ((mytextvar).length > maxlimit) ?
(((mytextvar).substring(0,maxlimit-3)) + '...') :
mytextvar }
</Text>
numberOfLines
在Text
组件上使用参数:<Text numberOfLines={1}>long long long long text<Text>
将产生:
long long long…
(假设您有短宽度的容器。)
ellipsizeMode
参数将省略号移动到head
或middle
。tail
是默认值。<Text numberOfLines={1} ellipsizeMode='head'>long long long long text<Text>
将产生:
…long long text
注意:该Text
组件还应包括style={{ flex: 1 }}
何时需要使用相对于其容器大小的省略号。对行布局等有用。
您可以使用ellipsizeMode和numberOfLines。例如
<Text ellipsizeMode='tail' numberOfLines={2}>
This very long text should be truncated with dots in the beginning.
</Text>
<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>
const styles = theme => ({
contentClass:{
overflow: 'hidden',
textOverflow: 'ellipsis',
display: '-webkit-box',
WebkitLineClamp:1,
WebkitBoxOrient:'vertical'
}
})
render () {
return(
<div className={classes.contentClass}>
{'content'}
</div>
)
}