开发者

Custom UITextField blurred text

开发者 https://www.devze.com 2022-12-22 12:04 出处:网络
When a UITextField with custom frame (200 × 54 px) loses focus, the text looks blurred. (source: i开发者_如何转开发dzr.org)

When a UITextField with custom frame (200 × 54 px) loses focus, the text looks blurred.

Custom UITextField blurred text

(source: i开发者_如何转开发dzr.org)

Is this a common problem? Is there any workaround / solution known?


I found a number of references to this bug through Google, but everybody worked around it by playing with font sizes. After much hunting I found this thread that says anti-aliasing is applied when a view's frame contains fractional pixel values, e.g. if you calculate its size as a fraction of the super view.

Sure enough, casting the CGRect values to int for the view's frame worked perfectly. So as an example, if you wanted your text field to be centered vertically in the superview, you should use an int cast like this:

textFieldWidth = 300;
textFieldHeight = 31;
offsetX = 0;
offsetY = (superview.bounds.size.height - textFieldHeight) / 2;

textField.frame = CGRectMake((int) offsetX,
                             (int) offsetY,
                             (int) textFieldWidth,
                             (int) textFieldHeight);

There is also the CGRectIntegral function that you can use to convert a CGRect to integral values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号