我有一个带有自定义字体的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也有同样的问题.
当使用按钮操作切换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)
| 归档时间: |
|
| 查看次数: |
2239 次 |
| 最近记录: |