131

The Text input is center aligned, how to fix this text input so that it takes input from top left corner

The Text input is center aligned, how to fix this text input so that it takes input from top left corner

Here is my css for text input:

 /* The Text input is center aligned, how to fix this text input so that it takes input from top left corner */

input: {
    flex: 1,
    padding: 4,
    marginRight: 1,
    marginTop: 5,
    fontSize: 18,
    borderWidth: 1,
    borderRadius: 4,
    borderColor: '#E6E5ED',
    backgroundColor: '#F8F8F9',
    justifyContent: 'flex-start',
    height: 150
}
2
  • 2
    umm... align it with what? Your question doesn't specify what you're trying to do. Commented Apr 10, 2015 at 11:20
  • 1
    what should i add in my css so that my text starts from the top left corner? Commented Apr 10, 2015 at 11:24

16 Answers 16

352

I had the same issue, but the above notes didn't solve it. There's an android-only style property textAlignVertical that fixes this issue on multiline inputs.

i.e. textAlignVertical: 'top'

Sign up to request clarification or add additional context in comments.

10 Comments

is ios solved by default or does this fix work only for android ?
@dev_ter it's android-only. I think iOS is top-aligned by default, though it's been a while since I've used RN so haven't actually confirmed. Not sure if/how you'd middle-align but feel free to make a note or edit if you find out how!
Awesome, solved the TextInput align problem with multiline={true} in android.
How it can be accepted answer if textAlignVertical is only for Android?
Working in iOS too
|
50

TextInput has default padding, override it by setting:

paddingTop: 0,
paddingBottom: 0

Github Issue

3 Comments

That was my issue. Also, textAlignVertical isn't a prop according to documentation
@shoopi It is, but it's documented under the multiline prop.
textAlignVertical works
35

I have found the solution that in Android, TextInput style textAlignVertical: 'top' works. but in ios, TextInput prop multiline={true} works.

3 Comments

I don't understand how people are happy with the multiline={true} "solution" on iOS. I see this suggestion everywhere but what if I wanna have a single line input that's vertically aligned?
@Georgios set numberOfLines to 1?
@AliMertCakar numberOfLines={1} doesn't work together multiline={true}. Press enter on your keyboard and you'll see it changes the line.
24

The Above Answers either give the for iOS or android, which can be quite misleading so this fixes it for both of the platfoms.

<TextInput
style={{
flex: 1,
width: "100%",
height: 150,
color: "#FFF",
textAlignVertical: "top", // android fix for centering it at the top-left corner 
}}
multiline={true} // ios fix for centering it at the top-left corner 
numberOfLines={10}
/>

For Android -

style={{
//...
flex:1,
textAlignVertical: "top", // android fix for centering it at the top-left corner 
//...
}}

For iOS, add multiline={true} to the <TextInput/> component

Comments

10

Give textAlignVertical : "top" that will solve your issue.

<TextInput placeholder="Your text post" multiline={true} numberOfLines={10}, maxLength={1000} style={{ borderWidth: 1, backgroundColor: "#e3e3e3", textAlignVertical : "top" }} />

Comments

5

It worked for me (RN 0.61.5):

<TextInput style={{textAlignVertical:'top', paddingTop: 0, paddingBottom:0 }} />

Comments

5

I had a similar use case in my iOS app, wherein the TextInput's height was 100 and the placeholder was showing in middle. I used multiline={true} and it made the text appear from the top.

1 Comment

is there a way to make it appear at the bottom?
3

Update 2015-07-03: multiline text inputs have now been merged:

https://github.com/facebook/react-native/pull/991

The multiline examples that ship with React Native in the UI Explorer should work as documented.

The problem you'll have is that multiline TextInput aren't working correctly yet, and the docs are misleading. Please see this Github issue:

https://github.com/facebook/react-native/issues/279

"We haven't ported that functionality to open source yet."

There is some code in that issue that gives minimal multiline functionality, so you might be able to get it working with that.

Comments

2

I found out per element inspector, that for iOS there is a paddingTop: 5 for multiline TextInputs. This was still applied even though I set paddingVertical: 15 for all of my inputs. The result was a not centered text in multiline inputs on iOS. The solution was to additionally add a paddingTop: 15 if the TextInput is multiline and the platform is iOS. Now there is visually no difference between single line and multiline inputs on both platforms, Android and iOS.

Comments

2

Apparently the solution for iOS is not as trivial as one might think.

While on Android you can just add this style:

paddingTop: 0,
paddingBottom: 0,

On iOS you'd need this:

multiline={true}

But what if you want a single line? This is my workaround:

<TextInput
   value={comment}
   onChangeText={setComment}
   {/*The lines below are the important ones*/}
   multiline={true}
   blurOnSubmit={true}
   autoCorrect={false}
   onSubmitEditing={handleOnSubmit}
/>

Let me explain what's happening here:

  1. As we said, multiline={true} vertically aligns the text.
  2. To avoid adding a new line after pressing the submit button, you need blurOnSubmit={true}. This also closes the keyboard and although in my case that's ok, I haven't figured a way around it, unfortunately.
  3. I used autoCorrect={false} because without it, the submit button will change the TextInput's value to the automatically selected suggestion (if any).
  4. Finally, if you want to perform some action when the submit button is pressed, then you need onSubmitEditing={handleOnSubmit}.

What a journey to just align a text...

1 Comment

Thanks, I was looking for a single line input text where I was always observing multi-line, But I found it here for a single line and it is working fine.
2

enter image description here enter image description here

import {TextInput, Dimensions} from 'react-native';

const {height, width} = Dimensions.get('window'); 


<TextInput 
style={{ borderWidth:1, borderRadius:10,minHeight:100, textAlignVertical: 'top',
 width: width-20,paddingHorizontal:10,  }}
          placeholder={"Enter Email ID "}
          multiline          
        />

Comments

1

Just Incase you are looking for code:

<TextInput
placeholder={'comment goes here!'}
multiline
style={{textAlignVertical:'top', ...otherStyle}}
/>

Comments

1

For those using nativewind, I avoided that the decenders got cutted by playing with the lineheight property. I.e. For me adding the class leading-5 works well.

Comments

0
import { Dimensions} from 'react-native';
const screenWidth = Math.round(Dimensions.get('window').width);
const screenHeight = Math.round(Dimensions.get('window').height);

// ...
intext: {
    height:screenHeight - 10,
    textAlignVertical: 'top',
    fontSize:17,
    padding:12,
    fontFamily:'courier',
    margin:10,     
},

Comments

0

I think it's default for iOS, for android in my case adding paddingVertical: 0, to text style worked.

Comments

-2

To get text aligned vertically center on both platforms use:

For android use textAlignVertical: "center"

For ios use justifyContent: "center"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.