MouzmiSadiq
MouzmiSadiq

Reputation: 2189

UITextField text not appearing at the center

I am just using a UITextField and the problem is the text in the UITextField do not appears at the center.It is placed at the top position of my textField.Is there any default settings that i am missing out.

thanks in advance.

Upvotes: 12

Views: 27411

Answers (5)

2ank3th
2ank3th

Reputation: 3109

For swift

To align vertically use

textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center 

To align horizontally use

quantity.textAlignment = NSTextAlignment.center

Upvotes: 1

shmakova
shmakova

Reputation: 6426

Swift 4:

// vertical alignment
textField.contentVerticalAlignment = .center

// horizontal alignment
textField.textAlignment = .center

Upvotes: 4

iPatel
iPatel

Reputation: 47049

For vertical alignment:

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

Centered both vertically and horizontally:

theTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
theTextField.textAlignment = NSTextAlignmentCenter;

Centered within the parent container:

YourTextFieldName.center = parentViewField.center;

Upvotes: 31

R2D2
R2D2

Reputation: 2640

Constant UITextAlignmentCenter is now deprecated as of iOS6 and above.

You should use:

textField.textAlignment = NSTextAlignmentCenter;

Upvotes: 15

MouzmiSadiq
MouzmiSadiq

Reputation: 2189

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

This one line code works perfectly to me

Upvotes: 0

Related Questions