[React Native]高度自增长的TextInput组件
生活随笔
收集整理的這篇文章主要介紹了
[React Native]高度自增长的TextInput组件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
之前我們學(xué)習(xí)了從零學(xué)React Native之11 TextInput了解了TextInput相關(guān)的屬性。
在開發(fā)中,我們有時候有這樣的需求, 希望輸入?yún)^(qū)域的高度隨著輸入內(nèi)容的長度而增長, 如下:
這時候我們需要自定義一個組件:
在項目中創(chuàng)建AutoExpandingTextInput.js
在多行輸入(multiline={true})的時候,可以通過onChange回調(diào)函數(shù),獲取內(nèi)容的高度event.nativeEvent.contentSize.height,然后修改內(nèi)容的高度。
接下來修改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"} });當(dāng)然我們知道在IOS端TextInput/Text組件默認不會水平居中的,需要在外層額外嵌套一層View,可以參考從零學(xué)React Native之10Text
運行結(jié)果:
更多精彩請關(guān)注微信公眾賬號likeDev
總結(jié)
以上是生活随笔為你收集整理的[React Native]高度自增长的TextInput组件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分页探究--Filter+JSTL
- 下一篇: mac下pip install 安装只能