Change UIFont in secure UITextField strange behaviour in iOS7
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.
In viewDidLoad:
[self.passwordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
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];
}
}
}
UITextField secureTextEntry bullets with a custom font?
A subclass that works this issue around. Create an arbitrary UITextField
, then set the secure
property to YES
(via KVC in IB).
Actually it implements a comment suggested by lukech. When textfield ends editing, it switches to an arbitrary textfield, then set a bulk of dots into, and some hack in text
accessor to always get the actual text the field holds.
@interface SecureTextFieldWithCustomFont : UITextField
@property (nonatomic) BOOL secure;
@property (nonatomic, strong) NSString *actualText;
@end
@implementation SecureTextFieldWithCustomFont
-(void)awakeFromNib
{
[super awakeFromNib];
if (self.secureTextEntry)
{
// Listen for changes.
[self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(editingDidChange) forControlEvents:UIControlEventEditingChanged];
[self addTarget:self action:@selector(editingDidFinish) forControlEvents:UIControlEventEditingDidEnd];
}
}
-(NSString*)text
{
if (self.editing || self.secure == NO)
{ return [super text]; }
else
{ return self.actualText; }
}
-(void)editingDidBegin
{
self.secureTextEntry = YES;
self.text = self.actualText;
}
-(void)editingDidChange
{ self.actualText = self.text; }
-(void)editingDidFinish
{
self.secureTextEntry = NO;
self.actualText = self.text;
self.text = [self dotPlaceholder];
}
-(NSString*)dotPlaceholder
{
int index = 0;
NSMutableString *dots = @"".mutableCopy;
while (index < self.text.length)
{ [dots appendString:@"•"]; index++; }
return dots;
}
@end
May be augmented to work with non NIB instantiations, handling default values, etc, but you probably get the idea.
UITextField secureTextEntry bullets with a custom font?
A subclass that works this issue around. Create an arbitrary UITextField
, then set the secure
property to YES
(via KVC in IB).
Actually it implements a comment suggested by lukech. When textfield ends editing, it switches to an arbitrary textfield, then set a bulk of dots into, and some hack in text
accessor to always get the actual text the field holds.
@interface SecureTextFieldWithCustomFont : UITextField
@property (nonatomic) BOOL secure;
@property (nonatomic, strong) NSString *actualText;
@end
@implementation SecureTextFieldWithCustomFont
-(void)awakeFromNib
{
[super awakeFromNib];
if (self.secureTextEntry)
{
// Listen for changes.
[self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(editingDidChange) forControlEvents:UIControlEventEditingChanged];
[self addTarget:self action:@selector(editingDidFinish) forControlEvents:UIControlEventEditingDidEnd];
}
}
-(NSString*)text
{
if (self.editing || self.secure == NO)
{ return [super text]; }
else
{ return self.actualText; }
}
-(void)editingDidBegin
{
self.secureTextEntry = YES;
self.text = self.actualText;
}
-(void)editingDidChange
{ self.actualText = self.text; }
-(void)editingDidFinish
{
self.secureTextEntry = NO;
self.actualText = self.text;
self.text = [self dotPlaceholder];
}
-(NSString*)dotPlaceholder
{
int index = 0;
NSMutableString *dots = @"".mutableCopy;
while (index < self.text.length)
{ [dots appendString:@"•"]; index++; }
return dots;
}
@end
May be augmented to work with non NIB instantiations, handling default values, etc, but you probably get the idea.
iOS7 UItextfield text disappear while coming from background
Its because I set border style to none because when I change it to other it works fine
UITextField with secure entry, always getting cleared before editing
Set,
textField.clearsOnBeginEditing = NO;
Note: This won't work if secureTextEntry = YES. It seems, by default, iOS clears the text of secure entry text fields before editing, no matter clearsOnBeginEditing is YES or NO.
NSAttributedString double ff counted as single f when using system font in iOS 7
It is to be expected. This is a feature of the font called a ligature.
Check out NSLigatureAttributeName
for options related to ligatures.
Related Topics
A Server with the Specified Hostname Could Not Be Found
Push View Programmatically in Callback, Swiftui
"Unable to Validate Your Application Error" While Uploading a New Version of iOS App
Can't Import Embedded Framework with Xcode 6 Gm
Storyboard Instantiateviewcontrollerwithidentifier Not Setting Iboutlets
#Import <Libxml/Tree.H> File Not Found After Xcode Update
Add a Scrollview to Existing View
How to Get the Current Application Icon in iOS
Sfspeechrecognizer - Detect End of Utterance
Change Uifont in Secure Uitextfield Strange Behaviour in iOS7
What Is Correct Format of Bundle Identifier in iOS
How to Test for the Class of a Variable in Swift
How to Increase Width of Textfield According to Typed Text
Why Are Animations on Bounds of an Uilabel Only Working When Increasing the Size
Uilabel: Background Dependent Color
Check If an Auto-Renewable Subscription Is Still Valid