ios UITextField 文本没有出现在中心
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14743786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITextField text not appearing at the center
提问by SumiSadiq
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.
我只是使用 a UITextField
,问题是中的文本UITextField
没有出现在中心。它位于我的 textField 的顶部位置。是否有任何我遗漏的默认设置。
thanks in advance.
提前致谢。
回答by iPatel
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;
回答by Richard
Constant UITextAlignmentCenter
is now deprecated as of iOS6 and above.
UITextAlignmentCenter
从 iOS6 及更高版本开始,常量现在已被弃用。
You should use:
你应该使用:
textField.textAlignment = NSTextAlignmentCenter;
回答by shmakova
Swift 4:
斯威夫特 4:
// vertical alignment
textField.contentVerticalAlignment = .center
// horizontal alignment
textField.textAlignment = .center
回答by 2ank3th
For swift
为迅速
To align vertically use
垂直对齐使用
textField.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
To align horizontally use
水平对齐使用
quantity.textAlignment = NSTextAlignment.center
回答by SumiSadiq
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
This one line code works perfectly to me
这一行代码对我来说完美无缺