简体   繁体   English

在相同的UITextField中居中占位符,但大小不同

[英]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 . 我正在尝试将相同的占位符文本居中于同一UITextField两个不同大小。

The UITextField in question is 243 wide in Portrait and 360 in Landscape. 有问题的UITextField在Portrait中为243宽,Landscape为360。

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. 当我旋转到“风景”并且UITextField重绘得更大时,它不再居中。 It's like 25% to the left (I am guessing in the middle of the Portrait length) 大约是25%(我猜是在人像长度的中间)

Is there any way to re-center the placeholder every time the length of a UITextField changes ? 每当UITextField的长度更改时,是否有任何方法可以将占位符重新居中?

interesting. 有趣。 5 minutes after I posted this question an idea came to mind. 在我发布此问题5分钟后,我想到了一个主意。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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