繁体   English   中英

UITextField中的占位符和文本位置不同(缩进不同)

[英]Placeholder and Text in UITextField are not in the same positions (different indent)

为什么UITextField中的占位符和文本具有不同的缩进? 占位符的文本似乎向右移了几个像素。 如何删除该缩进? 文本字段是在IB中创建的。 在此处输入图片说明

这里没有任何区别! 在此处输入图片说明

为了移动它使用

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;

您可以覆盖这两种方法来制作所需的矩形。 他们来了 :

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;

链接在这里

正如苹果所说:

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

我发现了这种行为的原因。 这似乎是iOS 5.0中的错误。 我创建了一个新项目,然后将UITextFields放到屏幕上,并且占位符的行为方式相同。 但是,当我将平台从5.0更改为6.1时,它起作用了! 占位符不再从文本转移了。 所以我认为这是iOS 5.0的错误。 谢谢大家的回答!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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