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. });

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...