react native实现可展开Text控件

前端之家收集整理的这篇文章主要介绍了react native实现可展开Text控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转载:http://blog.csdn.net/jan8705_/article/details/52279533

原理组件初始化时不设置Text控件的numberOfLines属性,测量一下组件高度(最大高度),然后在设置numberOfLines属性,再次测量一下组件高度(最小高度),若最大高度大与最小高度,表示需要显示“展开”。

[plain] view plain copy
  1. /*eslintnew-cap:["error",{"capIsNew":false}]*/
  2. importReact,{
  3. Component,
  4. PropTypes,
  5. }from'react';
  6. import{View,Image,StyleSheet,Animated,Text}from'react-native';
  7. exportdefaultclassCollapsibleTextextendsComponent{
  8. staticpropTypes={
  9. style:Text.propTypes.style,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> expandTextStyle:Text.propTypes.style,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> numberOfLines:PropTypes.number
  10. }
  11. constructor(props){
  12. super(props);
  13. this.state={
  14. /**文本是否展开*/
  15. expanded:true,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> numberOfLines:null,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> /**展开收起文字是否处于显示状态*/
  16. showExpandText:false,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> expandText:'展开',248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> /**是否处于测量阶段*/
  17. measureFlag:true
  18. this.numberOfLines=props.numberOfLines;
  19. /**文本是否需要展开收起功能:(实际文字内容是否超出numberOfLines限制)*/
  20. this.needExpand=true;
  21. this.measureFlag=true;
  22. }
  23. _onTextLayout(event){
  24. if(this.measureFlag){
  25. if(this.state.expanded){
  26. this.maxHeight=event.nativeEvent.layout.height;
  27. this.setState({expanded:false,numberOfLines:this.numberOfLines});
  28. }else{
  29. this.mixHeight=event.nativeEvent.layout.height;
  30. if(this.mixHeight==this.maxHeight){
  31. this.needExpand=false;
  32. this.needExpand=true;
  33. this.setState({showExpandText:true})
  34. this.measureFlag=false;
  35. _onPressExpand(){
  36. if(!this.state.expanded){
  37. this.setState({numberOfLines:null,expandText:'收起',expanded:true})
  38. }else{
  39. this.setState({numberOfLines:this.numberOfLines,expandText:'展开',expanded:false})
  40. render(){
  41. const{numberOfLines,onLayout,expandTextStyle,...rest}=this.props;
  42. letexpandText=this.state.showExpandText?(
  43. <Text
  44. style={[this.props.style,styles.expandText,expandTextStyle]}
  45. onPress={this._onPressExpand.bind(this)}>
  46. {this.state.expandText}</Text>
  47. ):null;
  48. return(
  49. <View>
  50. <Text
  51. numberOfLines={this.state.numberOfLines}
  52. onLayout={this._onTextLayout.bind(this)}
  53. {...rest}
  54. >
  55. {this.props.children}
  56. </Text>
  57. {expandText}
  58. </View>
  59. );
  60. conststyles=StyleSheet.create({
  61. expandText:{
  62. color:'blue',108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> marginTop:0
  63. });
原文链接:https://www.f2er.com/react/305084.html

猜你在找的React相关文章