[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 传值方式之 :子组件通过调用 其父组件传来的方法 传值回其父组件
随机推荐
- Django项目:CRM(客户关系管理系统)--53--44PerfectCRM实现账号快速注册登陆
# gbacc_ajax_urls.py # ————————42PerfectCRM实现AJAX全局账号注册———————— from django.conf.urls import url fro ...
- bzoj月赛1805
题目在最后,FG还不会做,等着$NicoDafaGood$和$Achen$给我讲 A 对于每一个质因子建一棵线段树,直接查询就好了 主要是看到所有数的大小都不是很大,然后质因子最多只有log个,复杂度 ...
- TZ_11_Spring-Boot的入门
1.SpringBoot的特点 为基于Spring的开发提供更快的入门体验 开箱即用,没有代码生成,也无需XML配置.同时也可以修改默认值来满足特定的需求 提供了一些大型项目中常见的非功能性特性,如嵌 ...
- poj1160 动态规划
#include<stdio.h> #include<string.h> #define INF 999999999 #define Min(x,y) (x<y?x:y) ...
- C++怎么读入非文本文件中的内容
C++怎么读入非文本文件中的内容 3条回答 #include <io.h> #include <windows.h> void main() { char* pFileName ...
- LintCode_41 最大子数组
题目 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1] ...
- ArrayList,LinkedList,Vestor
Collection是最基本的集合接口,声明了适用于JAVA集合的通用方法,list和set都继承自collection接口. Collection接口的方法 boolean add(Object o ...
- GIT生成公钥和私钥
转载至:https://blog.csdn.net/gwz1196281550/article/details/80268200 打开 git bash! git config --global us ...
- Django中的orm的惰性机制
惰性机制:Publisher.objects.all()或者.filter()等都只是返回了一个QuerySet(查询结果集对象)[https://www.cnblogs.com/chaojiying ...
- 核K-均值聚类(Kernel K-means Clustering)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wxcdzhangping/article/details/31366143 问题: 设 ...