在Swift 1.2和2.0中编辑时,UiTextField会更改字体

mat*_*ode 8 uikit ios

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

Ale*_*der 10

我遇到了同样的问题并找到了解决方案.问题归结为setSecureTextEntry在设置时更改字体,而在未设置时不更改字体.事实上,只要你UITextField有第一个响应者,就永远不能改变字体.

诀窍是resignFirstResponder在打电话之前setSecureTextEntry:,然后becomeFirstResponder再.这起作用(从iOS 9.2开始),但它会触发键盘显示/隐藏动画,并会导致屏幕"抖动".要解决这个问题,你还需要杀死键盘动画.

这是我的完整解决方案:

- (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];
        }
    }];
}
Run Code Online (Sandbox Code Playgroud)

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


Abh*_*eet 7

当使用按钮操作切换UiTextField的secureTextEntry时,我有一个奇怪的字体改变其大小和字体类型的情况. 在此输入图像描述 必须使用以下代码行显式管理UiTextField的字体:

password.font = UIFont(name: "systemFont", size: 14)
password.font = UIFont.systemFontOfSize(14)
Run Code Online (Sandbox Code Playgroud)

显示密码按钮中使用的完整代码:

//Function associated with the button for show password option
@IBAction func switchShowPasswordAction(sender: AnyObject) {
        if showPassword{
            showPassword  = false
            password.secureTextEntry = !showPassword

        }else{
            showPassword = true
            password.secureTextEntry = !showPassword
        }

        //Changing font fix
        password.font = UIFont(name: "systemFont", size: 14)
        password.font = UIFont.systemFontOfSize(14)
}
Run Code Online (Sandbox Code Playgroud)

发布应用此更改: 在此输入图像描述