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

猜你在找的React相关文章