ios 如何在 Swift 中居中 UITextField 文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24588939/
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-31 01:14:29  来源:igfitidea点击:

How to center UITextField text in Swift

iosuitextfieldswift

提问by almel

I have a UITextField object with the text property set to "hi". However these two lines have no affect on where "hi" is located

我有一个文本属性设置为“hi”的 UITextField 对象。但是这两行对“hi”所在的位置没有影响

 myUITextObject.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
 myUITextObject.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center

Is this a bug in Swift? If so how do I go about logging this as a bug so Apple can fix it in the next Swift release?

这是 Swift 中的错误吗?如果是这样,我该如何将此记录为错误,以便 Apple 可以在下一个 Swift 版本中修复它?

回答by Dave Wood

You would want to write it like this:

你会想这样写:

myUITextObject.textAlignment = .center

(Edited to change from .Center to .center)

(编辑为从 .Center 更改为 .center)

回答by klcjr89

What you're looking for is the textAlignment property; try this:

您正在寻找的是 textAlignment 属性;尝试这个:

myUITextObject.textAlignment = NSTextAlignmentCenter;

myUITextObject.textAlignment = NSTextAlignmentCenter;

回答by Snoobie

Another way to do it, is by using the storyboard.

另一种方法是使用故事板。

In fact there is a Vertical and Horizontal Control that you can use for align your element inside the UITextField. In the case above that align the text to the top left:

实际上,您可以使用垂直和水平控件在UITextField. 在上面的情况下,将文本与左上角对齐:

enter image description here

Hope it helps,

在此处输入图片说明

希望能帮助到你,