UITextField text does not appear in the center

I just use UITextField, and the problem is that the text in UITextFielddoes not appear in the center . It is placed at the top of my text box. There are default settings that I skip.

early.

+8
source share
5 answers

For vertical alignment:

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

It is centered both vertically and horizontally:

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

It is centered inside the parent container:

YourTextFieldName.center = parentViewField.center;
+27
source

The constant is UITextAlignmentCenternow deprecated with iOS6 and above.

You should use:

textField.textAlignment = NSTextAlignmentCenter;
+15
source

Swift 4:

// vertical alignment
textField.contentVerticalAlignment = .center

// horizontal alignment
textField.textAlignment = .center
+2

textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center 

quantity.textAlignment = NSTextAlignment.center
+1
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

0

All Articles