且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

占位符文本不以编程方式创建的UITextField为中心

更新时间:2023-11-17 16:18:58

您需要添加:

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.

如何垂直居中

由于原始问题已更新,请求如何垂直对齐占位符文本,并且答案隐藏在评论中,以下是如何执行此操作:

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;