React Native 之TextInput 高度自增长扩展实现

前端之家收集整理的这篇文章主要介绍了React Native 之TextInput 高度自增长扩展实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

新建一组件 ImageEqualEnlarge.js

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React,{ Component } from 'react';
import {
  StyleSheet,Text,TextInput,View
} 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){
    this.setState({
       text:event.nativeEvent.text,height:event.nativeEvent.contentSize.height,});
  }

  render() {
    return (
      <TextInput {...this.props}
       multiline={true}
       onChange={this.onChange}
       style={[styles.textInputStyle,{height:Math.max(35,this.state.height)}]}
       value={this.state.text}
       >
      </TextInput>
    );
  }


}

const styles = StyleSheet.create({
  textInputStyle:{
    fontSize:40,width:300,height:30,alignItems: 'center',backgroundColor:'grey',paddingTop:0,paddingBottom:0
  }

});

在index.android.js 引用
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React,{ Component } from 'react';
import {
  AppRegistry,StyleSheet,Image,TouchableHighlight,StatusBar,View
} from 'react-native';

import AutoExpandingTextInput from './AutoExpandingTextInput'

export default class ViewProject extends Component {

     _onChangeText(newText) {
        console.log('inputed text:' + newText);
    }

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


}



const styles = StyleSheet.create({
  container: {
    flex:1,justifyContent: 'center',backgroundColor:'#F5FCFF'
  }
});
AppRegistry.registerComponent('ViewProject',() => ViewProject);

原文链接:https://www.f2er.com/react/304422.html

猜你在找的React相关文章