在 iOS7 中更改安全 UITextField 中的 UIFont 奇怪行为 [英] Change UIFont in secure UITextField strange behaviour in iOS7

查看:27
本文介绍了在 iOS7 中更改安全 UITextField 中的 UIFont 奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的项目:https://github.com/edzio27/textFieldExample.git

I create a simple project: https://github.com/edzio27/textFieldExample.git

我在其中添加了两个 UITextField,一个带有登录名,第二个带有安全密码.我注意到有奇怪的行为:

where I add two UITextFields, one with login and second one with secure password. I've noticed there strange behaviour:

  1. 点击登录并添加一些文字,
  2. 点击密码并添加一些文字,
  3. 再次点击登录UITextField

请注意,密码字体大小有一个奇怪的行为.仅在iOS7中出现.

Notice that there is a strange behaviour in password font size. It is only appears in iOS7.

可能是什么问题?

谢谢.

推荐答案

正如几个人指出的那样,安全文本字段似乎并不总是能很好地使用自定义字体.我通过使用 UITextField 的 UIControlEventEditingChanged 来监视文本字段的更改并在输入任何内容时将其设置为系统字体(具有正常外观的项目符号点)来解决此问题,否则使用我的自定义字体.

As a couple people pointed out, it appears secure text fields don't always play well with custom fonts. I worked around this by using the UITextField's UIControlEventEditingChanged to monitor for changes to the textfield and set it to the system font (with the normal looking bullet points) when anything is entered, else use my custom font.

这允许我继续使用我的自定义字体作为文本字段占位符,并且在输入密码时仍然看起来不错.输入时一次显示一个的字符将是系统字体,但我可以接受.

This allows me to keep using my custom font for the textfield placeholder and still look good when the password is entered. The characters that show one-at-a-time while being entered will be the system font, but I'm okay with that.

在 viewDidLoad 中:

In viewDidLoad:

[self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

现在添加一个 textFieldDidChange 方法:

Now add a textFieldDidChange method:

- (void)textFieldDidChange:(id)sender
{
    UITextField *textField = (UITextField *)sender;

    if (textField == self.passwordTextField) {
        // Set to custom font if the textfield is cleared, else set it to system font
        // This is a workaround because secure text fields don't play well with custom fonts
        if (textField.text.length == 0) {
            textField.font = [UIFont fontWithName:@"OpenSans" size:textField.font.pointSize];
        }
        else {
            textField.font = [UIFont systemFontOfSize:textField.font.pointSize];
        }
    }
}

这篇关于在 iOS7 中更改安全 UITextField 中的 UIFont 奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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