之前我们学习了从零学React Native之11 TextInput了解了TextInput相关的属性。

在开发中,我们有时候有这样的需求, 希望输入区域的高度随着输入内容的长度而增长, 如下:



这时候我们需要自定义一个组件:

在项目中创建AutoExpandingTextInput.js

import React, {Component} from 'react';
import {AppRegistry, TextInput, StyleSheet} from 'react-native'; export default class AutoExpandingTextInput extends Component {
// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {
text: '',
height: 0
};
this.onChange = this.onChange.bind(this);
} onChange(event) {
console.log(event.nativeEvent);
this.setState({
text: event.nativeEvent.text,
height:event.nativeEvent.contentSize.height
});
}
onContentSizeChange(params){
console.log(params);
}
render() {
return (
<TextInput {...this.props} //将组件定义的属性交给TextInput
multiline={true}
onChange={this.onChange}
onContentSizeChange={this.onContentSizeChange}
style={[styles.textInputStyle,{height:Math.max(35,this.state.height)}]}
value={this.state.text}
/>
);
}
} const styles = StyleSheet.create({
textInputStyle: { //文本输入组件样式
width: 300,
height: 30,
fontSize: 20,
paddingTop: 0,
paddingBottom: 0,
backgroundColor: "grey"
}
});

在多行输入(multiline={true})的时候,可以通过onChange回调函数,获取内容的高度event.nativeEvent.contentSize.height,然后修改内容的高度。

接下来修改index.ios.js或者index.android.js 如下:

import AutoExpandingTextInput from './AutoExpandingTextInput';

class AwesomeProject extends Component {
_onChangeText(newText) {
console.log('inputed text:' + newText);
} render() {
return (
<View style={styles.container}>
<AutoExpandingTextInput
style={styles.textInputStyle}
onChangeText={this._onChangeText}
/>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
borderWidth: 1,
justifyContent: 'center',
alignItems: 'center'
},
textInputStyle: { //文本输入组件样式
width: 300,
height: 50,
fontSize: 20,
paddingTop: 0,
paddingBottom: 0,
backgroundColor: "grey"
}
});

当然我们知道在IOS端TextInput/Text组件默认不会水平居中的,需要在外层额外嵌套一层View,可以参考从零学React Native之10Text

运行结果:

更多精彩请关注微信公众账号likeDev

[React Native]高度自增长的TextInput组件的更多相关文章

  1. 基于React Native的Material Design风格的组件库 MRN

    基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...

  2. [RN] React Native 好用的时间线 组件

    React Native 好用的时间线 组件 效果如下: 实现方法: 一.组件封装 CustomTimeLine.js "use strict"; import React, {C ...

  3. [RN] React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法

    React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法 解决办法: 打开android工程,在AndroidManifest.xml中配置如下: <ac ...

  4. React Native(十三)——ios键盘挡住textInput

    渐入佳境 用React Native重构的项目也快接近尾声,剩下的就是适配ios的功能了.慢慢地也从中琢磨出了一点门道,于是就遇见了键盘遮挡textInput问题斑斑: 正常页面: android点击 ...

  5. React Native填坑之旅--Stateless组件

    Stateless component也叫无状态组件.有三种方法可以创建无状态组件. 坑 一般一个组件是怎么定义的: 很久以前的方法: const Heading = createClass({ re ...

  6. [React Native] State and Touch Events -- TextInput, TouchableHighLight

    In React, components manage their own state. In this lesson, we'll walk through building a component ...

  7. React Native 之轮播图swiper组件

    注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swipe ...

  8. React Native学习-调取摄像头第三方组件:react-native-image-picker

    近期做的软件中图片处理是重点,那么自然也就用到了相机照相或者相册选取照片的功能. react-native中有image-picker这个第三方组件,但是0.18.10这个版本还不是太支持iPad. ...

  9. react native 传值方式之 :子组件通过调用 其父组件传来的方法 传值回其父组件

随机推荐

  1. Codeforces 142D(博弈)

    要点 不难发现问题转化成:n堆石子,每次最多选k堆最少选1堆然后拿走一个石子,谁先没子可拿谁败.本题中撤退不必考虑. 就是记笔记吧,类似nim的博弈,举例:\[k=3,n=4\]\[4堆石子分别是1. ...

  2. 【python之路24】装饰器

    1.装饰器的应用场景 通常IT公司的程序开发是分工的,例如某公司某个部门负责底层函数的开发,另一个部门利用其函数实现高级功能,那么如果负责底层开发的函数需要改动,一般来说不会直接在函数上进行修改,通常 ...

  3. bzoj 1029 [JSOI2007]建筑抢修——贪心(伪dp)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1029 当然要按结束时间排序,然后按顺序修或跳过.就是那种“……不会使答案不优”的证明. 想了 ...

  4. loj6229 这是一道简单的数学题

    https://loj.ac/problem/6229 题解:https://blog.csdn.net/Vectorxj/article/details/79094659 套路推式子,杜教筛,证明复 ...

  5. 点击按钮使用window.open打开页面后,再次点击按钮会再打开一个页面,如何解决?

    点击按钮使用window.open打开页面后,再次点击按钮会再打开一个页面,如何解决? window.open("page1.html","win1"); 这句 ...

  6. ajax--表单带file数据提交报错Uncaught TypeError: Illegal invocation

    只要设置 contentType: false, //不设置内容类型 processData: false, //不处理数据 $("#btn").on("click&qu ...

  7. Scanner读取记事本文件内容为空的解决办法

    原因:记事本txt文件中含有中文,windows记事本编码方式为gbk,但是eclipse中为utf-8,所以需要在Scanner中指定编码方式.

  8. swagger暴露程序接口文档

    Swagger2是一个帮助用户.团队.企业快速.高效.准确地生产API服务的工具组件,同时还提供了部分测试功能,它的官方网站是https://swagger.io/. 1.引入Maven <de ...

  9. GYM 101350 F. Monkeying Around

    F. Monkeying Around time limit per test 2.0 s memory limit per test 256 MB input standard input outp ...

  10. 洛谷P1969 [NOIP2013提高组Day2T1] 积木大赛

    P1969 积木大赛 题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前, ...