Placeholder text not centered for UITextField created programmatically

后端 未结 4 1946
粉色の甜心
粉色の甜心 2020-12-08 04:14

I am creating a UITextField programmatically and placing it inside a UIView. The font size is set to 15.0f (system font). But the placeholder that appears is not centered in

4条回答
  •  春和景丽
    2020-12-08 04:36

    You need to add:

    txtField.textAlignment = NSTextAlignmentCenter; // Pre-iOS6 SDK: UITextAlignmentCenter
    

    Keep in mind, this adjusts both the alignment of the placeholder text as well as the text the user will enter in.

    How to center vertically

    Since the original question was updated to request how to vertically align the placeholder text and that answer is buried in the comments, here is how to do that:

    txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    

提交回复
热议问题