react native实现可展开Text控件

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

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

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

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

猜你在找的React相关文章