Change UIFont in secure UITextField strange behaviour in iOS7(在 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:
- 点击登录并添加一些文字,
- 点击密码并添加一些文字,
- 再次点击登录
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 奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 iOS7 中更改安全 UITextField 中的 UIFont 奇怪行为
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
