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

Iphone UITextField中的占位符和文本不在相同的位置(不同的缩进),iphone,ios,Iphone,Ios,为什么UITextField中的占位符和文本具有不同的缩进?占位符的文本似乎向右移动了几个像素。如何删除此缩进? 文本字段是在IB中创建的。 您可以覆盖这两种方法,以生成所需的rect。这是: - (CGRect)textRectForBounds:(CGRect)bounds; - (CGRect)placeholderRectForBounds:(CGRect)bounds; 链接是 正如苹果所说: **placeholderRectForBounds:** Returns the dra

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

您可以覆盖这两种方法,以生成所需的rect。这是:

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

您可以重写这2个方法以生成所需的rect。这是:

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

这里没什么区别!!

为了移动它,请使用

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

这里没什么区别!!

为了移动它,请使用

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

我找到了这种行为的原因。这似乎是iOS 5.0中的一个bug。我创建了一个新项目,然后将UITextFields放到屏幕上,占位符的行为方式也一样。但是当我把我的平台从5.0改为6.1时,它成功了!占位符不再从文本移动。 所以我认为这是iOS 5.0的一个缺陷。
谢谢大家的回答

我找到了这种行为的原因。这似乎是iOS 5.0中的一个bug。我创建了一个新项目,然后将UITextFields放到屏幕上,占位符的行为方式也一样。但是当我把我的平台从5.0改为6.1时,它成功了!占位符不再从文本移动。 所以我认为这是iOS 5.0的一个缺陷。
谢谢大家的回答

你能发布截图吗?请显示你的代码和截图是的,当然!我加了一张照片。这是来自模拟器的2个文本字段的屏幕截图。第一个包含占位符,第二个包含我在第一个位置键入的数字。郭绿川,这个文本字段是在IB.txtField.textAlignment=NSTextAlignmentCenter中创建的;你能发布截图吗?请显示你的代码和截图是的,当然!我加了一张照片。这是来自模拟器的2个文本字段的屏幕截图。第一个包含占位符,第二个包含我在第一个位置键入的数字。郭绿川,这个文本字段是在IB.txtField.textAlignment=NSTextAlignmentCenter中创建的;你没动他们?这是在IB中创建后的屏幕截图?是的。它来自IB。我删除了两个文本字段,并将“30”作为文本添加到其中一个字段中,作为占位符添加到另一个字段中。您没有移动它们吗?这是在IB中创建后的屏幕截图?是的。它来自IB。我删除了两个文本字段,并在其中一个字段中添加“30”作为文本,在另一个字段中添加“30”作为占位符