之前我们学习了从零学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. python \r \t \n 各种转义字符

    转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \t 横向制 ...

  2. 图像通道、Scalar、分离、合成通道

    http://lib.csdn.net/article/opencv/33264 http://blog.csdn.net/laohu_tiger/article/details/17359777 h ...

  3. [Array]122. Best Time to Buy and Sell Stock II(obscure)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. H5C3--属性选择器、兄弟选择器、伪类选择器

    属性选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  5. JS---元素隐藏的不同方式

    元素隐藏的不同方式 dispaly, visibility, opacity, height&border 为0 <!DOCTYPE html> <html lang=&qu ...

  6. mybatis深入理解(八)-----关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  7. 论ul、ol和dl的区别

    1.ul是无序列表,也就是说没有排列限制可以随意加li: <ul> <li>可以随意放置</li> <li>可以随意放置</li> < ...

  8. ubuntu 12.4,搞定apt源

    http://wiki.ubuntu.org.cn/Template:12.04source deb http://cn.archive.ubuntu.com/ubuntu/ precise main ...

  9. flask的基本操作

    常用的SQLAlchemy字段类型 # coding:utf-8 from flask import Flask from flask_sqlalchemy import SQLAlchemy app ...

  10. day37 09-Struts2和Hibernate整合环境搭建

    <!-- 设置本地Session --> <property name="hibernate.current_session_context_class"> ...