31

So when using, text input with multiline=true, I have this issue where the text is vertically centered instead of being pushed to the top.

This issue happens on both ios and android, except android has another issue where, when multiple lines are entered, they get letterboxed to the height of 1 line.

I'd like to point out, I have tried adding textAlignVertical: 'top' to the style of the textinput

Code: (I have this as a seperate copmonent as I use it in forms with form text but all parameters are passed something)

    <TextInput
            style={styles.input}
            value={value}
            autoComplete={autoComplete}
            autoCapitalize={autoCapitalize}
            placeholder={placeholder}
            secureTextEntry={secureTextEntry}
            keyboardType={keyboardType}
            returnKeyType={returnKeyType}
            autoFocus={autoFocus}
            onChangeText={onChangeText}
            onSubmitEditing={onSubmitEditing}
            multiline={multiline || false}
            ref={(r) => { inputRef && inputRef(r); }}
    />

styles:

input: {
    paddingRight: 10,
    lineHeight: 23,
    flex: 2,
    textAlignVertical: 'top'
},

ios screenshot

android screenshot

ketaki Damale
  • 634
  • 7
  • 25
Red
  • 393
  • 2
  • 5
  • 9

5 Answers5

79

If anyone facing the same issue, try textAlignVertical: "top" This works. For more info try https://github.com/facebook/react-native/issues/13897

Subhendu Kundu
  • 3,618
  • 6
  • 26
  • 57
19

i tried this for you please let me know if its correct

 <TextInput
            style={styles.input}
            value={this.state.value}
            onChangeText={text=>this.setState({value:text})}
            multiline={true}
            underlineColorAndroid='transparent'
    />

style as

  input: {
    width:200,
    borderBottomColor:'red',
    borderBottomWidth:1,
},

and expo link may be help you click here

chetan godiya
  • 286
  • 2
  • 13
3

So it turned out the View surrounding the textinput had alignItems: 'center' which centered the text within the text input.

So changed that to alignItems: this.multiline ? 'flex-start' : 'center',

Also for the android issue, I had to add numberOfLines={5} which fixed the letterboxing

Red
  • 393
  • 2
  • 5
  • 9
1

Setting the multiline prop solved the problem.

   <TextInput
        style={styles.input}
        value={this.state.value}
        onChangeText={text=>this.setState({value:text})}
        multiline={true}
        numberOfLines={4}
   />
Colin Wang
  • 6,778
  • 5
  • 26
  • 42
0

in my case:

 textAlignVertical: multiline ? 'top' : 'center',