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

查看:126
本文介绍了在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

  1. click on login and add some text,
  2. click on password and add some text,
  3. click again to login 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天全站免登陆