UITextField blurred text(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 模糊文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UITextField 模糊文本


- Android viewpager检测滑动超出范围 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01