[React Native]高度自增长的TextInput组件
生活随笔
收集整理的這篇文章主要介紹了
[React Native]高度自增长的TextInput组件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
之前我們學習了從零學React Native之11 TextInput了解了TextInput相關的屬性。
在開發中,我們有時候有這樣的需求, 希望輸入區域的高度隨著輸入內容的長度而增長, 如下:
這時候我們需要自定義一個組件:
在項目中創建AutoExpandingTextInput.js
在多行輸入(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}><AutoExpandingTextInputstyle={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组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分页探究--Filter+JSTL
- 下一篇: mac下pip install 安装只能