[React Native]高度自增长的TextInput组件
之前我们学习了从零学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组件的更多相关文章
- 基于React Native的Material Design风格的组件库 MRN
基于React Native的Material Design风格的组件库.(为了平台统一体验,目前只打算支持安卓) 官方网站 http://mrn.js.org/ Github https://git ...
- [RN] React Native 好用的时间线 组件
React Native 好用的时间线 组件 效果如下: 实现方法: 一.组件封装 CustomTimeLine.js "use strict"; import React, {C ...
- [RN] React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法
React Native 键盘管理 在Android TextInput遮盖,上移等问题解决办法 解决办法: 打开android工程,在AndroidManifest.xml中配置如下: <ac ...
- React Native(十三)——ios键盘挡住textInput
渐入佳境 用React Native重构的项目也快接近尾声,剩下的就是适配ios的功能了.慢慢地也从中琢磨出了一点门道,于是就遇见了键盘遮挡textInput问题斑斑: 正常页面: android点击 ...
- React Native填坑之旅--Stateless组件
Stateless component也叫无状态组件.有三种方法可以创建无状态组件. 坑 一般一个组件是怎么定义的: 很久以前的方法: const Heading = createClass({ re ...
- [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 ...
- React Native 之轮播图swiper组件
注释:swiper组件是第三方组件 所以在使用之前应该先在命令行安装,然后将第三方的模块引入(第三方模块地址:https://github.com/leecade/react-native-swipe ...
- React Native学习-调取摄像头第三方组件:react-native-image-picker
近期做的软件中图片处理是重点,那么自然也就用到了相机照相或者相册选取照片的功能. react-native中有image-picker这个第三方组件,但是0.18.10这个版本还不是太支持iPad. ...
- react native 传值方式之 :子组件通过调用 其父组件传来的方法 传值回其父组件
随机推荐
- Leetcode173. Binary Search Tree Iterator二叉搜索树迭代器
实现一个二叉搜索树迭代器.你将使用二叉搜索树的根节点初始化迭代器. 调用 next() 将返回二叉搜索树中的下一个最小的数. 注意: next() 和hasNext() 操作的时间复杂度是O(1),并 ...
- curl下载安装与使用
下载: https://curl.haxx.se/download.html 安装: 二进制安装.即解压即可. 使用 1.获取页面内容. 不加任何参数时,默认会发送GET请求来获取url内容到标准输出 ...
- kindle电子书下载网站收藏
kindle推 http://readfree.me 鸠摩捜书 新浪爱问共享资料 我的小书屋 云海图书馆 书语者 36镇网站收藏夹
- fidder下载及使用
fidder 下载地址:https://www.telerik.com/download/fiddler 安装采用默认方式安装即可.
- mybatis深入理解(二)-----Mybatis数据源与连接池
对于ORM框架而言,数据源的组织是一个非常重要的一部分,这直接影响到框架的性能问题.本文将通过对MyBatis框架的数据源结构进行详尽的分析,并且深入解析MyBatis的连接池.本文首先会讲述MyBa ...
- 通信网络 ccf
试题编号: 201709-4 试题名称: 通信网络 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 某国的军队由N个部门组成,为了提高安全性,部门之间建立了M条通路,每条通路只 ...
- SPSS20.O---软件安装
统计要与大量的数据打交道,涉及繁杂的计算和图表绘制.现代的数据分析工作如果离开统计软件几乎是无法正常开展.在准确理解和掌握了各种统计方法原理之后,再来掌握几种统计分析软件的实际操作,是十分必要的. 常 ...
- Leetcode590N-ary Tree Postorder TraversalN叉树的后序遍历
给定一个 N 叉树,返回其节点值的后序遍历. class Node { public: int val; vector<Node*> children; Node() {} Node(in ...
- Leetcode637.Average of Levels in Binary Tree二叉树的层平均值
给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. class Solution { public: vector<double> averageOfLevels(TreeNode ...
- Java IO:为什么InputStream只能读一次
http://zhangbo-peipei-163-com.iteye.com/blog/2021879 InputStream的接口规范就是这么设计的. /** * Reads the next b ...