UiTextField在Swift 1.2&amp ;;编辑时更改字体。 2.0 [英] UiTextField changes font while editing in Swift 1.2 & 2.0

查看:115
本文介绍了UiTextField在Swift 1.2&amp ;;编辑时更改字体。 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义字体的UITextField,一切正常,直到Swift更新到1.2和2.0。之后,每次我尝试编辑文本字段时,它都会将其字体更改为另一个看起来像Times New Roman的字体。有没有人有这方面的经验?

I have a UITextField with a custom font, everything worked fine until Swift update to 1.2 and 2.0. Afterwards, each time I try to edit a text field, it changes its font to a different one that seems a sort of Times New Roman. Does anyone have experience of that?

推荐答案

我遇到了同样的问题并想出了一个解决方案。问题归结为 setSecureTextEntry 在设置时更改字体,并在未设置时不再更改字体。事实上,只要您的 UITextField 有第一响应者,就永远不会改回字体。

I came across this same issue and figured out a solution. The problem boils down to setSecureTextEntry changing the font when it is set, and not changing it back when it is unset. In fact, you can never change the font back as long as your UITextField has first responder.

诀窍是到 resignFirstResponder 之前再次调用 setSecureTextEntry:然后再次 becomeFirstResponder 。此 工作(从iOS 9.2开始),但它会触发键盘显示/隐藏动画,并会导致屏幕抖动。为了解决这个问题,你也需要杀死键盘动画。

The trick is to resignFirstResponder before you call setSecureTextEntry: and then becomeFirstResponder again. This will work (as of iOS 9.2), but it triggers the keyboard show/hide animation and will cause the screen to "shake". To get around that, you'll need to kill the keyboard animation as well.

这是我的完整解决方案:

Here's my full solution:

- (void)setSecureTextEntry:(BOOL)secureTextEntry {

    __weak typeof(self) weakSelf = self;
    [UIView performWithoutAnimation:^{

        BOOL resumeResponder = NO;
        if([[weakSelf textEntryField] isFirstResponder]) {
            resumeResponder = YES;
            [[weakSelf textEntryField] resignFirstResponder];
        }

        [[weakSelf textEntryField] setSecureTextEntry:secureTextEntry];

        if(resumeResponder) {
            [[weakSelf textEntryField] becomeFirstResponder];
        }
    }];
}

PS:这不是Swift错误。这是一个UIKit错误。我对Objective-C也有同样的问题。

PS: This isn't a Swift bug. It's a UIKit bug. I had the same issue with Objective-C.

这篇关于UiTextField在Swift 1.2&amp ;;编辑时更改字体。 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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