React Native 实现动态计算文本高度

前端之家收集整理的这篇文章主要介绍了React Native 实现动态计算文本高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

  1.  
1.需求

在实际手机开发中经常遇到,需要动态计算文本的高度,尤其在文本列表中,当然你可以实现动态计算ListView 的cell的高度, 请参考: react native 实现动态高度Listview 和图文混排,但是现在我们的目标是通过普通的组件View 和 text 实现动态计算文本,怎么实现呢?

2.实现思路

1.通过计算每个Item的title 和 text 的高度,比较最大的高度,通过设置item的state改变自身的高度

2.通过Item的回调函数,将每个item 的高度,传给 Item所在的组件
3.item的父组件通过回调过来的Item的高度,通过过滤,计算所有Item的总高度
4.item的父组件 改变自身state,设置自身高度

3.代码实现

item.js

  1. export default class Item extends Component {
  2. static defaultProps = {
  3. title:'',text:'',itemCount:0,titleStyle:View.propTypes.style,textStyle:View.propTypes.style,viewStyle:View.propTypes.style,callBackItemHeight:PropTypes.func
  4. }
  5. static propTypes ={
  6. title:PropTypes.string,text:PropTypes.string,itemCount:PropTypes.number,titleStyle:Text.propTypes.style,textStyle:Text.propTypes.style,callBackItemHeight:PropTypes.func
  7. }
  8. constructor(props) {
  9. super(props);
  10. this.titleHeight=0;
  11. this.textHeight=0;
  12. this.state={
  13. itemHeight:40
  14. }
  15. }
  16. _titleLayout(event) {
  17. this.titleHeight=event.nativeEvent.layout.height
  18. // this.getItemHeight(this.titleHeight,this.textHeight)
  19. }
  20. _textLayout(event) {
  21. this.textHeight=event.nativeEvent.layout.height
  22. this.getItemHeight(this.titleHeight,this.textHeight)
  23. }
  24. getItemHeight(titleHeight,textHeight){
  25. let maxHeight=titleHeight>textHeight?titleHeight:textHeight
  26. this.setState({
  27. itemHeight:maxHeight
  28. })
  29. if(this.props.callBackItemHeight){
  30. this.props.callBackItemHeight(maxHeight)
  31. }
  32. this.refs.title.setNativeProps({
  33. style:{
  34. top:(this.state.itemHeight-titleHeight)/2,left:16,width:134,backgroundColor:'transparent',height:titleHeight,justifyContent:'center'
  35. }
  36. });
  37. this.refs.text.setNativeProps({
  38. style:{
  39. left:10,top:(this.state.itemHeight-textHeight)/2,width:Globle.window.width-170,height:textHeight,justifyContent:'center'
  40. }
  41. });
  42. this.refs.seperate.setNativeProps({
  43. style:{
  44. position:'absolute',width:Globle.window.width-16,height:1,backgroundColor:'#1a1a1a',top:this.state.itemHeight-1
  45. }
  46. })
  47. }
  48. render (){
  49. let container={height:this.state.itemHeight}
  50. return(
  51. <View style={[styles.item,container]}>
  52. <View ref="title">
  53. <Text style={{color:'#666666'}} onLayout={this._titleLayout.bind(this)}>
  54. {this.props.title}
  55. </Text>
  56. </View>
  57. <View ref="text">
  58. <Text style={{color:'#000000'}} onLayout={this._textLayout.bind(this)}>
  59. {this.props.text}
  60. </Text>
  61. </View>
  62. <View ref="seperate"></View>
  63. </View>
  64. )
  65. }
  66. }
  67. const styles=StyleSheet.create({
  68. item:{
  69. flexDirection:'row'
  70. }
  71. })

Index.android.js
  1. var heightArray=[]
  2. var hh=0
  3. export default class test extends Component {
  4. constructor(props) {
  5. super(props)
  6. this.state={
  7. dataSource:[],totalHeight:0
  8. }
  9. }
  10. componentWillMount(){
  11. this.setState({
  12. dataSource:[
  13. {title: '范冰冰',text:"近日,第11届亚洲电影大奖在香港落幕,《我不是潘金莲》获得包括最佳电影、最佳摄影在内的三项大奖,范冰冰凭借该片拿下最佳女主角奖。范冰冰在领奖台上表示,这是送给冯小刚导演的生日礼物,“你是我伯乐,我是你福将”"},{title: '陈数',text:"2009年,凭借《倾城之恋》入围韩国首尔电视节,唯一一位亚洲女演员获 韩国首尔电视节最佳女演员提名。2011年,获得第十七届上海电视节白玉兰奖“最佳女演员”,2011年第六届华鼎奖“最佳女主角”,2012年凭借《铁梨花》荣获“金鹰奖“观众喜爱女演员”奖项"},{title: '胡歌',text:"2005年1月24日,主演的古装仙侠剧《仙剑奇侠传》播出,胡歌饰演豪爽深情的李逍遥,并演唱两首插曲《六月的雨》和《逍遥叹》 "},{title: '何塞·保罗·贝塞拉·马希尔·儒尼奥尔(José Paulo Bezerra Maciel Júnior),简称保利尼奥[1] ,1988年7月25日出生于巴西圣保罗,巴西足球运动员。司职中前卫、后腰等。现效力于中国广州恒大淘宝足球俱乐部',text:"暴力鸟"}
  14. ]
  15. })
  16. }
  17. renderItem(data,i){
  18. return(<Item key={i}
  19. title={data.title}
  20. text={data.text}
  21. itemCount={this.state.dataSource.length}
  22. callBackItemHeight={(h)=>{
  23. console.log("h===>"+h)
  24. heightArray.push(h)
  25. hh+=h
  26. heightArray.sort().reverse()
  27. for(let i=0;i<this.state.dataSource.length;i++){
  28. hh+=heightArray[i]
  29. }
  30. this.setState({
  31. totalHeight:hh
  32. })
  33. }
  34. }
  35. />)
  36. }
  37. render() {
  38. return (
  39. <View >
  40. <View style={{backgroundColor:'white',top:0,height:this.state.totalHeight}}>
  41. {this.state.dataSource.map((data,i)=>this.renderItem(data,i))}
  42. </View>
  43. </View>
  44. );
  45. }
  46. }

4.代码实现效果





0
0

猜你在找的React相关文章