Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 403
MouzmiSadiq Avatar asked Feb 07 '13 04:02

MouzmiSadiq


4 Answers

Swift 4:

// vertical alignment
textField.contentVerticalAlignment = .center

// horizontal alignment
textField.textAlignment = .center
like image 191
shmakova Avatar answered Nov 15 '22 08:11

shmakova


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;
like image 21
iPatel Avatar answered Nov 15 '22 09:11

iPatel


For swift

To align vertically use

textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center 

To align horizontally use

quantity.textAlignment = NSTextAlignment.center
like image 43
2ank3th Avatar answered Nov 15 '22 08:11

2ank3th


Constant UITextAlignmentCenter is now deprecated as of iOS6 and above.

You should use:

textField.textAlignment = NSTextAlignmentCenter;
like image 42
R2D2 Avatar answered Nov 15 '22 10:11

R2D2