EarlGrey
EarlGrey

Reputation: 2543

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 ?

Upvotes: 1

Views: 1393

Answers (2)

EarlGrey
EarlGrey

Reputation: 2543

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.

Upvotes: 1

Adam Milligan
Adam Milligan

Reputation: 2826

You can probably accomplish the same result with:

[textfield setNeedsLayout];

or, possibly

[textfield setNeedsDisplay];

Upvotes: 0

Related Questions