为什么UITextField中的占位符和文本有不同的缩进?占位符的文本似乎向右移动了几个像素。我该如何删除这个缩进? 文本字段是在IB中创建的。
答案 0 :(得分:3)
这里没有任何区别!!
要移动它,请使用
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
答案 1 :(得分:1)
你可以覆盖这两个方法来制作你想要的矩形。他们在这里:
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
链接为here
正如Apple所说:
**placeholderRectForBounds:**
Returns the drawing rectangle for the text field’s placeholder text
- (CGRect)placeholderRectForBounds:(CGRect)bounds
Parameters
bounds
The bounding rectangle of the receiver.
Return Value
The computed drawing rectangle for the placeholder text.
Discussion
You should not call this method directly. If you want to customize the drawing rectangle for the placeholder text, you can override this method and return a different rectangle.
If the placeholder string is empty or nil, this method is not called.
**textRectForBounds:**
Returns the drawing rectangle for the text field’s text.
- (CGRect)textRectForBounds:(CGRect)bounds
Parameters
bounds
The bounding rectangle of the receiver.
Return Value
The computed drawing rectangle for the label’s text.
Discussion
You should not call this method directly. If you want to customize the drawing rectangle for the text, you can override this method and return a different rectangle.
The default implementation of this method returns a rectangle that is derived from the control’s original bounds, but which does not include the area occupied by the receiver’s border or overlay views.
答案 2 :(得分:0)
我发现了这种行为的原因。这似乎是iOS 5.0中的一个错误。我创建了一个新项目,然后将UITextFields放到屏幕上,占位符的行为方式相同。但是当我将我的平台从5.0改为6.1时,它确实有效!占位符不会长时间偏离文本。 所以我认为这是iOS 5.0的一个错误。 谢谢大家的回答!