0

为什么 UITextField 中的占位符和文本有不同的缩进?占位符的文本似乎向右移动了几个像素。我怎样才能删除这个缩进?文本字段是在 IB 中创建的。 在此处输入图像描述

4

3 回答 3

3

在这里没有任何区别!! 在此处输入图像描述

移动它使用

- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds;
于 2013-03-06T09:43:48.427 回答
1

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

- (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.
于 2013-03-06T09:40:05.917 回答
0

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

于 2013-03-06T10:23:00.497 回答