react native Text实现限制行数不显示省略号,尾部直接截断
2017-08-11 15:25
393 查看
react native Text实现限制行数,尾部直接截断。直接在下一行加省略号。
效果如图:
//超过三行就在底部加上省略号
用传统的方法只能实现在尾部加省略号,不过现在新版的RN,ellipsizeMode 属性配上
numberOfLines属性 也能实现这样的效果。不过对于老版的RN就无能为力了。刚好用的老版RN又要实现这样的效果就查资料然后想了想,然后就实现了一个这样的效果,废话不多说,上干货。
import React, { Component } from 'react';
import {
View,
Text,
Dimensions,
TouchableOpacity,
StyleSheet,
} from 'react-native';
const devicesWidth=Dimensions.get('window').width;
class MyText extends Component {
constructor(props) {
super(props);
this.state={
height:0,
tag:0,
};
}
textOnLayout(e){
const layout = e.nativeEvent.layout;
if(layout.height>60){ //行高是20然后把text的高度设置为60就能保证行数控制在3行了
this.refs.text.setNativeProps({
style:{
height:60,
width:devicesWidth-74,
}
});
this.setState({
tag:1,
});
}
}
render() {
return(
<View >
<Text
ref="text"
style={styles.text}
onLayout={this.textOnLayout.bind(this)}
>
{this.props.text}
</Text>
{this.state.tag===0?(<View></View>):(<Text style={styles.text1}>......</Text>)}
</View>
);}
}
const styles = StyleSheet.create({
text: {
fontSize:15,
lineHeight:20,
color: '#323232',
width:devicesWidth-74,
},
text1: {
fontSize:15,
lineHeight:20,
color: '#323232',
},
});
module.exports=MyText;
效果如图:
//超过三行就在底部加上省略号
用传统的方法只能实现在尾部加省略号,不过现在新版的RN,ellipsizeMode 属性配上
numberOfLines属性 也能实现这样的效果。不过对于老版的RN就无能为力了。刚好用的老版RN又要实现这样的效果就查资料然后想了想,然后就实现了一个这样的效果,废话不多说,上干货。
import React, { Component } from 'react';
import {
View,
Text,
Dimensions,
TouchableOpacity,
StyleSheet,
} from 'react-native';
const devicesWidth=Dimensions.get('window').width;
class MyText extends Component {
constructor(props) {
super(props);
this.state={
height:0,
tag:0,
};
}
textOnLayout(e){
const layout = e.nativeEvent.layout;
if(layout.height>60){ //行高是20然后把text的高度设置为60就能保证行数控制在3行了
this.refs.text.setNativeProps({
style:{
height:60,
width:devicesWidth-74,
}
});
this.setState({
tag:1,
});
}
}
render() {
return(
<View >
<Text
ref="text"
style={styles.text}
onLayout={this.textOnLayout.bind(this)}
>
{this.props.text}
</Text>
{this.state.tag===0?(<View></View>):(<Text style={styles.text1}>......</Text>)}
</View>
);}
}
const styles = StyleSheet.create({
text: {
fontSize:15,
lineHeight:20,
color: '#323232',
width:devicesWidth-74,
},
text1: {
fontSize:15,
lineHeight:20,
color: '#323232',
},
});
module.exports=MyText;
相关文章推荐
- android自定义实现textview限制多少行数以后以省略号结尾。 ... 这种省略号不是
- android EditText文本限制输入行数,多余部分显示省略号
- TextView限制最大行数,多出部分显示省略号,解决ellipsize = "end" 失效的问题
- Android中TextView实现超过固定行数显示“...展开全部”
- React Native 之TextInput 高度自增长扩展实现
- [置顶] android开发之当设置textview多少字后以省略号显示。限制TextView的字数
- Android 文字过长TextView如何自动截断并显示成省略号
- 微信小程序实现指定显示行数多余文字去掉用省略号代替
- textview显示字数的问题或者是限制显示多少行,超过用省略号表示
- 让TextView显示指定的行数,超过部分用省略号代替
- 限制EditText显示的行数
- 如何实现js展开收起(折叠)效果呢?例如:一段内容过长了,我限制他显示为20个字内,但是超出了20的就先用省略号代替,然后后面添加一个展开,点击展开后就显示所有的内容,这时候展开按钮就变成了 折叠按钮
- 关于TextView限制显示字符,多余用省略号显示问题
- 文本限制显示行数,多出的显示省略号...
- Android中的TextView实现多行显示省略号以及下划线的实现
- React Native 指定文字行数,超出行数显示更多
- TextView限制多行,超出内容时用省略号显示
- Android中的TextView实现多行显示省略号
- React-Native实现登录页面,并显示和清除用户的输入
- 使用 text-overflow: ellipsis 实现溢出文本省略号显示