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

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

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

诀窍是在调用setSecureTextEntry:之前resignFirstResponder setSecureTextEntry:然后再次becomeFirstResponder 。 这工作(截至iOS 9.2),但它会触发键盘显示/隐藏animation,并会导致屏幕“摇晃”。 为了解决这个问题,你还需要杀死键盘animation。

这是我的完整解决scheme:

 - (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]; } }]; } 

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

当使用button操作切换UiTextField的secureTextEntry时,我有一个奇怪的字体改变大小和字体types的情况。 在这里输入图像说明 必须通过使用以下代码行来显式pipe理UiTextField的字体:

 password.font = UIFont(name: "systemFont", size: 14) password.font = UIFont.systemFontOfSize(14) 

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

 //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) } 

应用此更改后: 在这里输入图像说明

由于我使用自定义字体,我们需要保留原始字体。 创build一个扩展到UITextField

 extension UITextField { func enablePasswordModeWithShowHide() { secureTextEntry = false let showButton = UIButton(type: UIButtonType.System) showButton.setTitle("HIDE", forState: .Normal) showButton.titleLabel?.textAlignment = .Right showButton.sizeToFit() rightView = showButton rightViewMode = .Always showButton.addTarget(self, action: "handleShowHideTapped", forControlEvents: .TouchUpInside) showButton.tintColor = UIColor.blackColor() } func handleShowHideTapped() { secureTextEntry = !secureTextEntry let font = self.font self.font = nil self.font = font if let oldText = text { text = ""; text = oldText; } if let button = rightView as? UIButton { button.setTitle(secureTextEntry ? "SHOW" : "HIDE", forState: .Normal) button.sizeToFit() } } } 

在哪里可以这样实施:

 passwordTextField.enablePasswordModeWithShowHide() 

在切换secureTextEntry标志后,使用自定义字体属性设置defaultTextAttributes

NSDictionary * attrsDictionary = @ {NSFontAttributeName:// customfont};
_passwordtextfield.defaultTextAttributes = attrsDictionary;

我必须应用以下解决scheme与最新的Xcode 7.1.1实际上在我的情况下工作我怀疑这个问题是框架。

 - (IBAction)btnPasswordShowAction:(id)sender { self.txtPassword.secureTextEntry = !self.txtPassword.secureTextEntry; NSString *tmpString = self.txtPassword.text; self.txtPassword.text = @" "; self.txtPassword.text = tmpString; [self.txtPassword setFont:nil]; self.txtPassword.font = [UIFont fontWithName:@"OpenSans-Regular" size:16.0]; } #pragma mark - Textfield Delegate Methods - (void)textFieldDidBeginEditing:(UITextField *)textField { [self.txtPassword setFont:nil]; self.txtPassword.font = [UIFont fontWithName:@"OpenSans-Regular" size:16.0]; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { [self.txtPassword setFont:nil]; self.txtPassword.font = [UIFont fontWithName:@"OpenSans-Regular" size:16.0]; return YES; } 

所有这些都可以解决很多问题,但是我必须使用不同的解决scheme来实现我所需要的结果,因为我正在使用自定义字体。

您需要在故事板检查器窗格中为UITextField设置attributed ,如下所示:

在检查员中选择归因于

然后,在代码中,您需要pipe理可见性的切换,每次设置属性文本,以确保格式正确。 我也辞去了第一反应(),只是为了照顾一些我还没弄清楚的定位故障。

 func toggleShowPass() { self.showing = !showing txtpassword.secureTextEntry = !showing textFieldPassword.resignFirstResponder() let string = textFieldPassword.text! let attrString = NSMutableAttributedString(string: string) textFieldPassword.addAttribute(NSFontAttributeName, value: UIFont(name: "AvenirNext-Regular", size: 16.0)!, range: NSMakeRange(0, string.characters.count)) textFieldPassword.attributedText = attrString }