Reputation: 14494
I have a UITextField
that is using the Museo Sans Rounded 300 font. 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 (i.e. becomes, and relinquishes, being the first responder).
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.
Upvotes: 20
Views: 5328
Reputation: 61
i just test result above, @Javier Peigneux's answer is the most concise
#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. hope will help you .
Upvotes: 2
Reputation: 4345
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. You should be setting the custom font and size in the viewWillAppear method as well. 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
.
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];
}
}
Upvotes: 0
Reputation: 567
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.
- (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];
}
}
Upvotes: 4
Reputation: 79
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. 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). 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). That's only for a split second per character though. If that is acceptable, then this solution works.
Upvotes: 2
Reputation: 9700
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
. 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.
Upvotes: 5