简体   繁体   中英

How to center UITextField text in Swift

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

 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?

You would want to write it like this:

myUITextObject.textAlignment = .center

(Edited to change from .Center to .center)

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

myUITextObject.textAlignment = NSTextAlignmentCenter;

, 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 :

在此处输入图片说明

Hope it helps,

斯威夫特 5 2021

yourTextField.attributedText = NSAttributedString(string: "String for textfield", attributes: [.paragraphStyle: paragraphStyle])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM