[英]How to center UITextField text in Swift
I have a UITextField object with the text property set to "hi".我有一个文本属性设置为“hi”的 UITextField 对象。 However these two lines have no affect on where "hi" is located
但是这两行对“hi”所在的位置没有影响
myUITextObject.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
myUITextObject.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center
Is this a bug in Swift?这是 Swift 中的错误吗? If so how do I go about logging this as a bug so Apple can fix it in the next Swift release?
如果是这样,我该如何将此记录为错误,以便 Apple 可以在下一个 Swift 版本中修复它?
You would want to write it like this:你会想这样写:
myUITextObject.textAlignment = .center
(Edited to change from .Center to .center) (编辑为从 .Center 更改为 .center)
What you're looking for is the textAlignment property;您正在寻找的是 textAlignment 属性; try this:
尝试这个:
myUITextObject.textAlignment = NSTextAlignmentCenter;
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
.实际上,您可以使用 Vertical 和 Horizontal Control 来对齐
UITextField
内的元素。 In the case above that align the text to the top left :在上面的情况下,将文本与左上角对齐:
斯威夫特 5 2021
yourTextField.attributedText = NSAttributedString(string: "String for textfield", attributes: [.paragraphStyle: paragraphStyle])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.