简体   繁体   English

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

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

Why placeholder and text in UITextField have different indents? 为什么UITextField中的占位符和文本具有不同的缩进? Placeholder's text seems to be shifted to right for a few pixels. 占位符的文本似乎向右移了几个像素。 How can i remove this indent? 如何删除该缩进? Text Field was created in IB. 文本字段是在IB中创建的。 在此处输入图片说明

It does not make any difference here!! 这里没有任何区别! 在此处输入图片说明

For moving it use 为了移动它使用

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

You can override these 2 method to make the rect you want. 您可以覆盖这两种方法来制作所需的矩形。 Here they are : 他们来了 :

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

The link is here 链接在这里

As Apple said : 正如苹果所说:

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

I found out the reason of this behavior. 我发现了这种行为的原因。 It seems to be a bug in iOS 5.0. 这似乎是iOS 5.0中的错误。 I created a new project then put UITextFields to screen and placeholders behave the same way. 我创建了一个新项目,然后将UITextFields放到屏幕上,并且占位符的行为方式相同。 But when i changed my platform from 5.0 to 6.1, it worked! 但是,当我将平台从5.0更改为6.1时,它起作用了! Placeholder is no long shifted from text. 占位符不再从文本转移了。 So i think it's a bug of iOS 5.0. 所以我认为这是iOS 5.0的错误。 Thanks all for answers! 谢谢大家的回答!

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

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