简体   繁体   中英

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

Why placeholder and text in UITextField have different indents? 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. 在此处输入图片说明

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. I created a new project then put UITextFields to screen and placeholders behave the same way. But when i changed my platform from 5.0 to 6.1, it worked! Placeholder is no long shifted from text. So i think it's a bug of iOS 5.0. Thanks all for answers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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