简体   繁体   中英

centering placeholder in the same UITextField, but of two different sizes

I am trying to have the same placeholder text centered in two different sizes of the same UITextField .

The UITextField in question is 243 wide in Portrait and 360 in Landscape.

When in Portrait the placeholder centers in the middle just fine. When I rotate to Landscape and the UITextField gets redrawn bigger, it is no longer centered. It's like 25% to the left (I am guessing in the middle of the Portrait length)

Is there any way to re-center the placeholder every time the length of a UITextField changes ?

interesting. 5 minutes after I posted this question an idea came to mind.

After the textfield gets redrawn in Portrait I do this:

Textfield.textAlignment = UITextAlignmentLeft;
Textfield.textAlignment = UITextAlignmentCenter;

After the textfield gets redrawn in Landscape I do this:

Textfield.textAlignment = UITextAlignmentRight;
Textfield.textAlignment = UITextAlignmentCenter;

Now the placeholder is perfectly centered in both modes. Hacky, but seems to work. Until someone replies with the "proper way", I guess.

You can probably accomplish the same result with:

[textfield setNeedsLayout];

or, possibly

[textfield setNeedsDisplay];

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