UITextField 模糊文本 [英] UITextField blurred text

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

问题描述

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


(来源:mikeweller.com) p>

有人知道如何解决这个问题吗?

解决方案

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

我通过 Google 找到了许多关于这个错误的参考,但每个人都通过调整字体大小来解决它.经过大量搜索,我发现 this thread 表示当视图的帧包含小数像素值时应用抗锯齿,例如如果您将其大小计算为超级视图的一部分.

果然,将视图框架的 CGRect 值转换为 (int) 效果很好.例如,如果您希望您的文本字段在超级视图中垂直居中,您应该使用这样的 (int) 类型转换:

<上一页>文本字段宽度 = 300;文本字段高度 = 31;偏移X = 0;offsetY = (superview.bounds.size.height - textFieldHeight)/2;textField.frame = CGRectMake((int) offsetX,(int) 偏移量,(int) 文本字段宽度,(int) textFieldHeight);

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

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:


(source: mikeweller.com)

Does anybody know how to fix this?

解决方案

OK I'm answering my own question here.

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.

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

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