在UITextField中使用自定义字体会导致它在访问时稍微移位 - 是否有修复? [英] Using a custom font in a UITextField causes it to shift slightly when accessed -- is there a fix?

查看:173
本文介绍了在UITextField中使用自定义字体会导致它在访问时稍微移位 - 是否有修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITextField中有一个自定义字体,我注意到当它被访问时(键盘出现时),文本向下移动非常少量 - 可能是一个像素或两三个。 (当然,我无法衡量它,但这足以让我注意到。)然后当键盘被解除时,它会再次向上移动。

I have a custom font in a UITextField, and I've noticed that when it's accessed (when the keyboard appears), the text shifts down by a very small amount -- maybe a pixel or two or three. (I've no way to measure it, of course, but it's enough for me to notice.) And then when the keyboard is dismissed, it shifts back up again.

我已经尝试在编辑时清除字段(这隐藏了初始向下移动的问题),但它还没有解决问题。我也查看了IB中的各种属性,并尝试改变一些,但问题仍然存在。我在模拟器和iPad2上得到了相同的结果。

I've tried clearing the field on editing (which hides the problem of the initial shift down), but it hasn't solved the problem. I've also looked over the various attributes in IB, and tried changing a few, but still the problem persists. I get the same results in the simulator and on my iPad2.

(该字段远离键盘,所以不是整个视图都移开了 - - 它只是该特定文本字段的内容。)

(The field is well clear of the keyboard, so it's not the entire view moving out of the way -- it's just the contents of that specific text field.)

我确定这是导致问题的自定义字体 - 没有它就不会发生。

I'm sure it's the custom font that's causing the problem -- it doesn't occur without it.

知道如何解决这个问题吗?我想我可能需要以编程方式创建文本字段,而不是在IB中 - 我知道我可能应该在尝试此问题之前尝试一下,但是我不愿意去解决所有问题,如果它不会解决问题。

Any idea how to address this? I was thinking I might need to create the text field programmatically, instead of in IB -- and I know I probably ought to try that before asking the question here, but I'm loathe to go to all that trouble if it won't solve the problem.

任何建议都值得赞赏!

推荐答案

我有这个问题也是如此。

I had this issue as well.

要修复,子类 UITextField 并实现以下方法来调整文本的定位不编辑和编辑。

To fix, subclass UITextField and implement the following methods to adjust the positioning of text when not editing and editing.

- (CGRect)textRectForBounds:(CGRect)bounds {

    return CGRectInset( bounds , 8 , 8 );
}

- (CGRect)editingRectForBounds:(CGRect)bounds {

    return CGRectInset( bounds , 8 , 5 );
}

这篇关于在UITextField中使用自定义字体会导致它在访问时稍微移位 - 是否有修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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