简体   繁体   English

使用自定义字体时,带有secureTextEntry的UITextField中的项目符号大小会随着焦点切换而改变大小

[英]Size of bullets in UITextField with secureTextEntry changes size as focus switches when using custom font

I have a UITextField that is using the Museo Sans Rounded 300 font. 我有一个使用Museo Sans Rounded 300字体的UITextField Everything works fine for normal UITextFields, but when you set the secureTextEntry = YES , then there's this disconcerting change to the size of the bullets as the UITextField gets and loses focus (ie becomes, and relinquishes, being the first responder). 一切都适用于普通的UITextFields,但是当你设置secureTextEntry = YES ,随着UITextField获得并失去焦点(即成为并放弃,成为第一个响应者),这会对子弹的大小产生令人不安的变化。

When the UITextField has focus, the bullets appear to be using the custom font, but once it loses focus they change to being these much bigger (standard size) bullets. UITextField具有焦点时,子弹似乎使用自定义字体,但一旦失去焦点,它们就会变为这些更大(标准尺寸)的子弹。

So, the only way I found to combat this was to use the textFieldDidBeginEditing and textFieldDidEndEditing delegate methods, keep track of what was entered in the text field, replace it with a mask of bullets, and disable secureTextEntry . 因此,我发现解决这个问题的唯一方法是使用textFieldDidBeginEditingtextFieldDidEndEditing委托方法,跟踪文本字段中输入的内容,用子弹掩码替换它,并禁用secureTextEntry So, when they leave the field, they're actually just seeing the right number of bullets, rather than their secured text. 因此,当他们离开战场时,他们实际上只是看到正确数量的子弹,而不是他们的安全文本。 It's hacky and messy, but it'll do for me, perhaps for you, too. 这是hacky和凌乱,但它也会为我做,也许对你而言。

I found an easy solution an it works quite good. 我找到了一个简单的解决方案,它运作得很好。 Basically you have to change the font to a custom font when you set secureTextEntry to yes. 基本上,当您将secureTextEntry设置为yes时,您必须将字体更改为自定义字体。

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    if([textField.text isEqual:@"Password"]){
        textField.text = @"";
        textField.font = [UIFont fontWithName:@"Helvetica" size:14.5];
        textField.secureTextEntry = YES;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{

    if([textField.text isEqual:@""]){
        textField.text = @"Password";
        textField.secureTextEntry = NO;
        textField.font = [UIFont fontWithName:@"YourFont" size:14.5];
    }
}

Another workaround: 另一种解决方法:

While this is an iOS bug (and new in iOS 7, I should add), I do have another way to work around it that one might find acceptable. 虽然这是一个iOS错误(在iOS 7中是新的,我应该添加),但我确实有另一种方法可以解决它可能会被接受的问题。 The functionality is still slightly degraded but not by much. 功能仍然略有下降但不是很多。

Basically, the idea is to set the font to the default font family/style whenever the field has something entered in it; 基本上,只要字段中输入了某些内容,就可以将字体设置为默认字体系列/样式; but when nothing is entered, set it to your custom font. 但是当没有输入任何内容时,请将其设置为自定义字体。 (The font size can be left alone, as it's the family/style, not the size, that is buggy.) Trap every change of the field's value and set the font accordingly at that time. (字体大小可以单独留下,因为它是系列/样式,而不是大小,有缺陷。)捕获字段值的每次更改并在此时相应地设置字体。 Then the faint "hint" text when nothing is entered has the font that you want (custom); 然后,当没有输入任何内容时,微弱的“提示”文本具有您想要的字体(自定义); but when anything is entered (whether you are editing or not) will use default (Helvetica). 但是当输入任何内容时(无论您是否正在编辑)都将使用默认值(Helvetica)。 Since bullets are bullets, this should look fine. 由于子弹是子弹,这应该看起来很好。

The one downside is that the characters, as you type before being replaced by bullets, will use default font (Helvetica). 一个缺点是,在被子弹替换之前键入的字符将使用默认字体(Helvetica)。 That's only for a split second per character though. 这只是每个角色的瞬间。 If that is acceptable, then this solution works. 如果这是可以接受的,则此解决方案有效。

i just test result above, @Javier Peigneux's answer is the most concise 我只是测试上面的结果,@ Javier Peigneux的回答是最简洁的

#pragma mark -- UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UCSSafeTF *)safeTF{

    safeTF.font = [UIFont fontWithName:@"Helvetica" size:14];
}

- (void)textFieldDidEndEditing:(UCSSafeTF *)safeTF{

    safeTF.font = [UIFont fontWithName:@"Helvetica" size:14];
}

now i write like this, and the result is OK. 现在我这样写,结果还可以。 then the reason why you see the bullets size change from small to big is very clear, just because apple iOS 10 below "help" us resize the custom font. 那么你看到子弹大小从小变大的原因非常明显,只是因为苹果iOS 10下面“帮助”我们调整自定义字体的大小。 hope will help you . 希望对你有所帮助。

Just create a method that gets called every time the show/hide password toggle is selected. 只需创建一个方法,每次选择显示/隐藏密码切换时都会调用该方法。 Inside the method, set the font to nil, then set the font to your custom font and font size. 在方法内部,将字体设置为nil,然后将字体设置为自定义字体和字体大小。 You should be setting the custom font and size in the viewWillAppear method as well. 您也应该在viewWillAppear方法中设置自定义字体和大小。 Inside this method, you're re-setting it. 在此方法中,您将重新设置它。

This way, you don't need to disable secureTextEntry (which could make your text field vulnerable) and you don't need to use textFieldDidBeginEditing or textFieldDidEndEditing . 这样,您不需要禁用secureTextEntry (这可能会使您的文本字段易受攻击),并且您不需要使用textFieldDidBeginEditingtextFieldDidEndEditing

Code Example: 代码示例:

//if the password is obscured and the toggle to show it has been turned on, display password.  Else, obscure it.
- (IBAction)togglePasswordVisibility:(id)sender {
    //  Xcode contains a bug where the font changes to default font if these two lines of code are not included.
    self.passwordInputTextField.font = nil;
    self.passwordInputTextField.font = [UIFont fontWithName:@"myCustomFontName" size:myDesiredFontSize];  //set this in viewWillAppear as well!

    if (self.passwordInputTextField.secureTextEntry == YES) {
        self.passwordInputTextField.secureTextEntry = NO;
        [self.showHideButton setTitle:@"HIDE" forState:UIControlStateNormal];
    } else {
        self.passwordInputTextField.secureTextEntry = YES;
        [self.showHideButton setTitle:@"SHOW" forState:UIControlStateNormal];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM