加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React-Native之TextInput实现高度自增长解决方案

发布时间:2020-12-15 07:31:02 所属栏目:百科 来源:网络整理
导读:前言 TextInput在部分业务场景下, 需要随着字体多少进行高度自行调节,那么这个效果如何实现呢? 方法 为了方便重用,我们定义一个公用的组件: class AutoExpandingInput extends Component { onContentSizeChange(event) { this .setState({height: event

前言

TextInput在部分业务场景下, 需要随着字体多少进行高度自行调节,那么这个效果如何实现呢?

方法

为了方便重用,我们定义一个公用的组件:

class AutoExpandingInput extends Component{
    onContentSizeChange(event) {
        this.setState({height: event.nativeEvent.contentSize.height});
    }
     render() {
        return (
            <TextInput {...this.props}  
                multiline={true}
                onChange={this.onChange}
                onContentSizeChange={this.onContentSizeChange.bind(this)}
                style={[styles.textInputStyle,{height:Math.max(35,this.state.height)}]}
                value={this.state.text}
            />
        );
    }
}

这里主要使用Math函数,和onContentSizeChange监听当前尺寸变化,更新state。

使用:

render() {
        return (
            <View style={styles.container}>
                <AutoExpandingInput  style={styles.textInputStyle} onChangeText={this._onChangeText} />
            </View>
        );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读