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.
Size of bullets in UITextField with secureTextEntry changes size as focus switches when using custom font
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.
Show # instead of bullets in UITextField for Secure Text Entry
- No you dont need to find any 3rd party library for this logic
- No there is no default option available for your need
Yes, you need to write a custom logic for your demand, So here it goes...
var passwordText = String()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == textFieldPassword {
var hashPassword = String()
let newChar = string.characters.first
let offsetToUpdate = passwordText.index(passwordText.startIndex, offsetBy: range.location)
if string == "" {
passwordText.remove(at: offsetToUpdate)
return true
}
else { passwordText.insert(newChar!, at: offsetToUpdate) }
for _ in passwordText.characters { hashPassword += "#" }
textField.text = hashPassword
return false
}
Swift 4:-
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == textFieldPassword {
var hashPassword = String()
let newChar = string.first
let offsetToUpdate = passwordText.index(passwordText.startIndex, offsetBy: range.location)
if string == "" {
passwordText.remove(at: offsetToUpdate)
return true
}
else { passwordText.insert(newChar!, at: offsetToUpdate) }
for _ in 0..<passwordText.count { hashPassword += "#" }
textField.text = hashPassword
return false
}
return true
}
Swift 4.2: UITextField secureEntryText using Image instead of Default Bullets
After two days struggling with this, I come to solution that can trick this. I'm creating stackView
that contain of UITextField
. I create array for temporary text that filled the UITextField
. Then, when the UITextField is filled by some character, I programmatically add UIImage
. In my code, will look like this.
let chars = Array(shieldString)
for (index, char) in chars.enumerated() {
if let tmp = self.stackView.arrangedSubviews[index] as? UITextField {
let imageView = UIImageView(image: UIImage(named: "dotImage")!)
imageView.frame = CGRect(x: 0, y: 0, width: 28, height: 28)
imageView.center = CGPoint(x: tmp.frame.size.width / 2, y: tmp.frame.size.height / 2)
imageView.tag = 100
tmp.addSubview(imageView)
}
}
And for clear the Image, just using this. I use tag so I can delete specific subview.
for ele in self.stackView.arrangedSubviews {
if let viewWithTag = self.view.viewWithTag(100) {
viewWithTag.removeFromSuperview()
}
}
I know that this not custom class, and quiet messy, but if you in a rush like me, this probably will save you.
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.
In iOS7 UITextField with secureTextEntry ruins layout in other text fields
... seconds later ...
Easiest solution is to be elegant and execute the self.text = ...
logic in -(void)textFieldDidBeginEditing:(UITextField *)textField
, it works flawlessly.
Related Topics
Class Has No Initializers Swift
Swift - How to Remove a Decimal from a Float If the Decimal Is Equal to 0
How to Find Bluetooth Audio Devices in iOS
Ios9 - This Application Is Modifying the Autolayout Engine from a Background Thread -- Where
Custom Uitableviewcell Selection Style
iOS 7/Xcode 5: Access Device Launch Images Programmatically
Change Text Color of Items in Uiactionsheet - iOS 8
How to Fetch All Contacts Record in iOS 9 Using Contacts Framework
How to Implement Uipageviewcontroller That Utilizes Multiple Viewcontrollers
How to Get Download Progress in Afnetworking 2.0
How to Add Http Headers in Request Globally for iOS in Swift
How to Send a Silent Push Notification Payload
Including Custom Data into iOS Crash Dumps
Why the Skstorereviewcontroller Does Not Let Me Submit a Review
React/Rctbridgemodule.H' File Not Found
iOS - Mkmapview Place Annotation by Using Address Instead of Lat/Long