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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:11:20  来源:igfitidea点击:

UITextField text not appearing at the center

iphoneiosobjective-c

提问by SumiSadiq

I am just using a UITextFieldand the problem is the text in the UITextFielddo 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 UITextAlignmentCenteris 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

这一行代码对我来说完美无缺