UITextField模糊文本 [英] UITextField blurred text

查看:185
本文介绍了UITextField模糊文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使使用标准字体大小,我也遇到了UITextField文本被模糊/消除锯齿的问题。当控件是第一响应者时,文本将显得清晰,但当失去焦点时,文本将再次模糊:

I am having a problem with a UITextField's text being blurred/anti-aliased even with a standard font size. The text will appear crisp when the control is the first responder, but blurred again when it loses focus:

alt text http://www.mikeweller.com/images/blog/UITextFieldBlur.png

有没有人知道怎么解决这个问题吗?

Does anybody know how to fix this?

推荐答案

好的我在这里回答我自己的问题。

OK I'm answering my own question here.

我通过谷歌发现了一些对这个bug的引用,但每个人都通过玩字体来解决这个问题。经过多次狩猎后,我找到了此线程表示当视图的帧包含小数像素值时会应用抗锯齿,例如如果你把它的大小计算为超级视图的一小部分。

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.

果然,将CGRect值转换为(int)视图的框架工作得很好。举个例子,如果你想让你的文本字段在superview中垂直居中,你应该使用这样的(int)强制转换:

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

还可以使用CGRectIntegral函数将CGRect转换为整数值。

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

这篇关于UITextField模糊文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆