Reputation: 2189
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
Reputation: 3109
For swift
To align vertically use
textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
To align horizontally use
quantity.textAlignment = NSTextAlignment.center
Upvotes: 1
Reputation: 6426
Swift 4:
// vertical alignment
textField.contentVerticalAlignment = .center
// horizontal alignment
textField.textAlignment = .center
Upvotes: 4
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
Reputation: 2640
Constant UITextAlignmentCenter
is now deprecated as of iOS6 and above.
You should use:
textField.textAlignment = NSTextAlignmentCenter;
Upvotes: 15
Reputation: 2189
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
This one line code works perfectly to me
Upvotes: 0