How to center UITextField text in Swift

后端 未结 3 1473
礼貌的吻别
礼貌的吻别 2021-01-01 10:48

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.contentVe         


        
相关标签:
3条回答
  • 2021-01-01 11:24

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

    myUITextObject.textAlignment = NSTextAlignmentCenter;

    0 讨论(0)
  • 2021-01-01 11:27

    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:



    Hope it helps,

    0 讨论(0)
  • 2021-01-01 11:31

    You would want to write it like this:

    myUITextObject.textAlignment = .center
    

    (Edited to change from .Center to .center)

    0 讨论(0)
提交回复
热议问题