Add a permanent Character to UITextField - ios

Is there a way to permanently add a letter to a UITextField where the user cannot delete it? I want to add a single character and the user not be able to delete it but they can still add letters after.
Cheers,
p.s. This is for iOS

A UITextField has a delegate method called should change characters in range, this method basically ask, should i add or remove the next character? and from that you can dictate what you would like. Here is some example code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL shouldChangeCharacters = ( (textField.text.length + string.length) > 1 );
return shouldChangeCharacters;
}
This code means if the new character being added plus the current text is greater than 1 then it is okay to make the change, if the text is not greater than one, then we will not make the change...
Now, under the assumption that someone may try to paste over your character, this delegate method is still called but you have to do a few things.
if (range.location == 0) {
NSString* firstCharacter = [string substringToIndex:1];
BOOL firstCharacterIsDesiredCharacter = [firstCharacter isEqualToString:#"#"];
if ( !firstCharacterIsDesiredCharacter ) {
NSString* symbolWithText = [NSString stringWithFormat:#"#%#",text];
// ******************** \\
[textView setText:symbolWithText];
return NO;
// or we could do this
string = symbolWithText;
// ******************** \\
}
}
Both scenarios, are modifying the values of the parameters... some people don't like doing that. It could be a bad programming practice.. or if you are going to modify them there's some process you should do first.
With that, we only need to run this code if they are trying to replace the first character, i substituted the hash tag symbol, the first character is from a range of location 0 and length of 1. So if the range is equal to 0, then we run our code to fix it. This code also takes into consideration that they might be pasting the special symbol with it. so if the UITextField read #work, and they tried to copy "work" or "#work" it takes both scenarios into consideration and completely skips the code if the hash mark is the first character.
UITextField Reference

try this
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = #"left";
label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = label;

Try using this:
in your .h:
IBOutlet UITextFeild *textFeild;
in your .m:
- (BOOL)textFieldShouldReturn:(UITextField *)textFeild {
NSString *textFeildText = textFeild.text;
textFeild.text = [NSString stringWithFormat:#"string you want to always add %#",textFeildText];
return NO;
}
This won't always show up when the user is editing the text box, but once they hit return it will automatically be added to the text box. If you use this code I recommend that you do not have the string that you want in the text field by default, or else you may end up with something like this:
String you want to add String you want to add Hello World!
If you just use the code I give you and don't put the string that you want to always be in the field by default, you're good to go! If you want to make the keyboard automatically disappear after they tap return just add this in the textFeildShouldReturn method above the 'return NO' statement:
[textFeild resignFirstResponder];

Related

Why does the font on iOS textfields changes upon altering secureTextEntry programatically? [duplicate]

I’m using a custom font in a UITextField, which has secureTextEntry turned on. When I’m typing in the cell, I see the bullets in my chosen font, but when the field loses focus, those bullets revert to the system standard font. If I tap the field again, they change back to my font, and so on.
Is there a way I can ensure that they continue to display the custom font’s bullets, even when the field is out of focus?
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.
For those having trouble with losing custom fonts when toggling secureTextEntry, I found a work-around (I'm using the iOS 8.4 SDK). I was trying to make a toggle for showing/hiding a password in a UITextField. Every time I'd toggle secureTextEntry = NO my custom font got borked, and only the last character showed the correct font. Something funky is definitely going on with this, but here's my solution:
-(void)showPassword {
[self.textField resignFirstResponder];
self.textField.secureTextEntry = NO;
}
First responder needs to be resigned for some reason. You don't seem to need to resign the first responder when setting secureTextEntry to YES, only when setting to NO.
The actual problem appears to be that the editing view (UITextField does not draw its own text while editing) uses bullets (U+2022) to draw redacted characters, while UITextField uses black circles (U+25CF). I suppose that in the default fonts, these characters look the same.
Here's an alternate workaround for anyone interested, which uses a custom text field subclass, but doesn't require juggling the text property or other special configuration. IMO, this keeps things relatively clean.
#interface MyTextField : UITextField
#end
#implementation MyTextField
- (void)drawTextInRect:(CGRect)rect
{
if (self.isSecureTextEntry)
{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = self.textAlignment;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:self.font forKey:NSFontAttributeName];
[attributes setValue:self.textColor forKey:NSForegroundColorAttributeName];
[attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
CGSize textSize = [self.text sizeWithAttributes:attributes];
rect = CGRectInset(rect, 0, (CGRectGetHeight(rect) - textSize.height) * 0.5);
rect.origin.y = floorf(rect.origin.y);
NSMutableString *redactedText = [NSMutableString new];
while (redactedText.length < self.text.length)
{
[redactedText appendString:#"\u2022"];
}
[redactedText drawInRect:rect withAttributes:attributes];
}
else
{
[super drawTextInRect:rect];
}
}
#end
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.
I found a trick for this issue.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField tag]== TAG_PASS || [textField tag]== TAG_CPASS)
{
// To fix password dot size
if ([[textField text] isEqualToString:#"" ])
{
[textField setText:#" "];
[textField resignFirstResponder];
[textField becomeFirstResponder];
[textField setText:#""];
}
}
}
[passWordTextField resignFirstResponder];
passWordTextField.secureTextEntry = !passWordTextField.secureTextEntry;
[passWordTextField becomeFirstResponder];
This is the fastest way to solve this bug!
iOS is acting a bit strange when it comes to custom fonts. Try removing "Adjust to Fit" for that textfield. If that doesn't work, I'm guessing that what bothering you is the size increase of the font.
A simple solution for that would be:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.secureTextEntry)
{
[textField setFont:[UIFont fontWithName:#"Helvetica-Bold" size:10.0]];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.secureTextEntry)
{
[textField setFont:[UIFont fontWithName:#"Helvetica-Bold" size:10.0]];
}
}
You'll need to play with the size a bit in order for it to look like there is no size change when loosing focus on the UITextField.
If you have a major spacing problem between characters like in the edited question, the simplest (and a bit ugly) solution would be to create a Bullet image that matches the above size & spacing and matches the amount of characters entered by the user that will appear when the user leaves the UITextField.
A secureTextEntry text field can be avoided altogether:
NSString *pin = #"";
BOOL pasting = FALSE;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(!pasting) {
pin = [pin stringByReplacingCharactersInRange:range withString:string];
// Bail out when deleting a character
if([string length] == 0) {
return YES;
}
pasting = TRUE;
[textField paste:#"●"];
return NO;
} else {
pasting = FALSE;
return YES;
}
}
I recommend to resignFirstResponder before you change scureTextEntry and then becomeFirstResponder again as it is posted here: https://stackoverflow.com/a/34777286/1151916
Swift 5 and iOS 14 is around but isSecureTextEntry set to true for custom font still displays the wrong size bullets, although the actual leading letter is of the correct size.
None of the solutions from stack overflow has worked for me except a hacky workaround of setting the font to the system font when password is in secure mode.
if textField.isSecureTextEntry {
self.textField.font = UIFont.systemFont(ofSize: 17)
} else {
self.textField.font = UIFont(name: "Roboto-Regular", size: 17)
}

How can I add different line spacing for different parts of NSAttributedString?

I want to have a single NSAttributedString contain multiple messages. If a single message has a long text and it wraps around, I want to have a line spacing of, say, 5. Because I have a single NSAttributedString contain multiple messages, I want to have a bigger line spacing between each message; let's say 20.
What I want
The 'I see' is one message. The 'I'd think it'd be both...' is one message, although it wraps down to two lines and 'Like a one way chat' is one message.
Notice how the line spacing between the 2nd and 3rd is smaller than the 1st and 2nd and between the 3rd and 4th.
What I've tried
I am appending a \n to the end of each message and I've tried using NSParagraphStyle which gives me control of the line spacing, but it seems to be all or nothing:
// index is the index of the group of messages as I iterate through them
// contentText is an NSMutableAttributedString
if index != messages.count - 1 {
let style = NSMutableParagraphStyle()
style.lineSpacing = 40.0
let lineReturn = NSMutableAttributedString(string: "\n")
contentText.appendAttributedString(lineReturn)
if index == 0 {
contentText.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(contentText.length-lineReturn.length, lineReturn.length))
} else {
contentText.addAttribute(NSParagraphStyleAttributeName, value: style, range: NSMakeRange(contentText.length-lineReturn.length-1, lineReturn.length+1))
}
}
If I add the line spacing to the beginning it will set the line spacing for the entire label.
if index == 0 {
let style = NSMutableParagraphStyle()
style.lineSpacing = 40.0
contentText.addAttribute(NSParagraphStyleAttributeName, value: style1, range: NSMakeRange(start, 1))
}
(This is really only my latest try.)
Thanks for any help! :)
Details
Have very basic custom markup in your English message so you can
parse out the different pieces
Instruct your translators to leave the markup in and translate the
rest Have a UIView that can serve as the container of this message
Break your English message up in pieces to separate the regular text
from the clickable text
For each piece create a UILabel on the container UIView
For the clickable pieces, set your styling, allow user interaction
and create your tap gesture recognizer
Do some very basic bookkeeping to place the words perfectly across
the lines
For Understand.
In the view controller's viewDidLoad I placed this:
[self buildAgreeTextViewFromString:NSLocalizedString(#"I agree to the #<ts>terms of service# and #<pp>privacy policy#",
#"PLEASE NOTE: please translate \"terms of service\" and \"privacy policy\" as well, and leave the #<ts># and #<pp># around your translations just as in the English version of this message.")];
I'm calling a method that will build the message. Note the markup I came up with. You can of course invent your own, but key is that I also mark the ends of each clickable region because they span over multiple words.
Here's the method that puts the message together -- see below. First I break up the English message over the # character (or rather #"#" string). That way I get each piece for which I need to create a label separately. I loop over them and look for my basic markup of <ts> and <pp> to detect which pieces are links to what. If the chunk of text I'm working with is a link, then I style a bit and set up a tap gesture recogniser for it. I also strip out the markup characters of course. I think this is a really easy way to do it.
Note some subtleties like how I handle spaces: I simply take the spaces from the (localised) string. If there are no spaces (Chinese, Japanese), then there won't be spaces between the chunks either. If there are spaces, then those automatically space out the chunks as needed (e.g. for English). When I have to place a word at the start of a next line though, then I do need to make sure that I strip of any white space prefix from that text, because otherwise it doesn't align properly.
- (void)buildAgreeTextViewFromString:(NSString *)localizedString
{
// 1. Split the localized string on the # sign:
NSArray *localizedStringPieces = [localizedString componentsSeparatedByString:#"#"];
// 2. Loop through all the pieces:
NSUInteger msgChunkCount = localizedStringPieces ? localizedStringPieces.count : 0;
CGPoint wordLocation = CGPointMake(0.0, 0.0);
for (NSUInteger i = 0; i < msgChunkCount; i++)
{
NSString *chunk = [localizedStringPieces objectAtIndex:i];
if ([chunk isEqualToString:#""])
{
continue; // skip this loop if the chunk is empty
}
// 3. Determine what type of word this is:
BOOL isTermsOfServiceLink = [chunk hasPrefix:#"<ts>"];
BOOL isPrivacyPolicyLink = [chunk hasPrefix:#"<pp>"];
BOOL isLink = (BOOL)(isTermsOfServiceLink || isPrivacyPolicyLink);
// 4. Create label, styling dependent on whether it's a link:
UILabel *label = [[UILabel alloc] init];
label.font = [UIFont systemFontOfSize:15.0f];
label.text = chunk;
label.userInteractionEnabled = isLink;
if (isLink)
{
label.textColor = [UIColor colorWithRed:110/255.0f green:181/255.0f blue:229/255.0f alpha:1.0];
label.highlightedTextColor = [UIColor yellowColor];
// 5. Set tap gesture for this clickable text:
SEL selectorAction = isTermsOfServiceLink ? #selector(tapOnTermsOfServiceLink:) : #selector(tapOnPrivacyPolicyLink:);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:selectorAction];
[label addGestureRecognizer:tapGesture];
// Trim the markup characters from the label:
if (isTermsOfServiceLink)
label.text = [label.text stringByReplacingOccurrencesOfString:#"<ts>" withString:#""];
if (isPrivacyPolicyLink)
label.text = [label.text stringByReplacingOccurrencesOfString:#"<pp>" withString:#""];
}
else
{
label.textColor = [UIColor whiteColor];
}
// 6. Lay out the labels so it forms a complete sentence again:
// If this word doesn't fit at end of this line, then move it to the next
// line and make sure any leading spaces are stripped off so it aligns nicely:
[label sizeToFit];
if (self.agreeTextContainerView.frame.size.width < wordLocation.x + label.bounds.size.width)
{
wordLocation.x = 0.0; // move this word all the way to the left...
wordLocation.y += label.frame.size.height; // ...on the next line
// And trim of any leading white space:
NSRange startingWhiteSpaceRange = [label.text rangeOfString:#"^\\s*"
options:NSRegularExpressionSearch];
if (startingWhiteSpaceRange.location == 0)
{
label.text = [label.text stringByReplacingCharactersInRange:startingWhiteSpaceRange
withString:#""];
[label sizeToFit];
}
}
// Set the location for this label:
label.frame = CGRectMake(wordLocation.x,
wordLocation.y,
label.frame.size.width,
label.frame.size.height);
// Show this label:
[self.agreeTextContainerView addSubview:label];
// Update the horizontal position for the next word:
wordLocation.x += label.frame.size.width;
}
}
if you want to use gesture then use this method.
- (void)tapOnTermsOfServiceLink:(UITapGestureRecognizer *)tapGesture
{
if (tapGesture.state == UIGestureRecognizerStateEnded)
{
NSLog(#"User tapped on the Terms of Service link");
}
}
- (void)tapOnPrivacyPolicyLink:(UITapGestureRecognizer *)tapGesture
{
if (tapGesture.state == UIGestureRecognizerStateEnded)
{
NSLog(#"User tapped on the Privacy Policy link");
}
}
Hope this helps. I'm sure there are much smarter and more elegant ways to do this, but this is what I was able to come up with and it works nicely.
this answer display output like following screen shot...but you got idea from this answer.
Gotcha!
You need to play with baselineOffset attribute:
let contentText = NSMutableAttributedString(
string: "I see\nI'd think it`d be both a notification and a\nplace to see past announcements\nLike a one way chat.")
contentText.addAttribute(.baselineOffset, value: 10, range: NSRange(location: 0, length: 5))
contentText.addAttribute(.baselineOffset, value: -10, range: NSRange(location: 85, length: 20))
Result:
"I see
I'd think it`d be both a notification and a
place to see past announcements
Like a one way chat."

UITextfield format ****user

I want to add masked and unmasked characters, both together in UITextfield.
e.g I have a UITextField called UserName "TestUser". Now I want to display username in the UITextField like "****User". Is it possible to add both text together? I want to do this thing while entering character in UITextfield.
It might be worth your while to look at the text field delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
It is called
"whenever the user types a new character in the text field or deletes an existing character"
So you can set the range 0-3 or whatever you like to "*" but you should hold the first characters somewhere else, which I believe to be possible before returning yes, with the textField parameter provided. I don't know if you tried this already or not and I haven't tried this myself, but let me know how it goes.
something I figure out and made below solution but still it is not exactly that I want. I want to mask/asterisks first four characters while entering values in UITextField. But here I am doing this thing on textFieldShouldReturn method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// Store orignal string - we can use later
_strOriginalString = textField.text;
//Make temporary Masked String
NSRange replacementRange = NSMakeRange(0, 4);
_strMaskString = [NSMutableString new];
for (int i = 0; i<4; i++) {
[_strMaskString appendString:#"●"];
}
//Replace Orignal String's Range with Masked string
self.txtUsername.text = [_strOriginalString stringByReplacingCharactersInRange:replacementRange withString:_strMaskString];
NSLog(#"Textfield string - %#",self.txtUsername.text);
NSLog(#"orignal string - %#",_strOriginalString);
NSLog(#"masked string - %#",_strMaskString);
return NO;
}
I don't think, that this is possible. Why do you want to show the name like "****User" in the UITextfield? You can use a label and mask the first characters with *

iOS - appending string before and after the word

I want to add a string in the highlighted area in the textview, I mean by the highlighted area, where the blue line is located.
So once the user click on the button it adds a string where the "blue line" is located
I used stringByAppendingString but it adds the string after the word exists only
NSRange range = myTextView.selectedRange;
NSString * firstHalfString = [myTextView.text substringToIndex:range.location];
NSString * secondHalfString = [myTextView.text substringFromIndex: range.location];
myTextView.scrollEnabled = NO; // turn off scrolling
NSString * insertingString = [NSString stringWithFormat:#"your string value here"];
myTextView.text = [NSString stringWithFormat: #"%#%#%#",
firstHalfString,
insertingString,
secondHalfString];
range.location += [insertingString length];
myTextView.selectedRange = range;
myTextView.scrollEnabled = YES;
You need to use the selectedRange to find out where the text cursor is. Then use replaceCharactersInRange:withString: or insertString:atIndex: to insert the new text into the original text. Then update the text into the view.
Even though its not clear what you are trying to achieve, it seems that you want the user to start editing the textfield from the position where text starts. In that case , you can refer following:
Hint 1
Set your view controller (or some other appropriate object) as the text field's delegate and implement the textFieldDidBeginEditing: method like this:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
UITextPosition *beginning = [textField beginningOfDocument];
[textField setSelectedTextRange:[textField textRangeFromPosition:beginning
toPosition:beginning]];
}
Note that setSelectedTextRange: is a protocol method of UITextInput (which UITextField implements), so you won't find it directly in the UITextField documentation.
Hint 2
self.selectedTextRange = [self textRangeFromPosition:newPos toPosition:newPos];
Hint 3
finding-the-cursor-position-in-a-uitextfield/

UITextField secureTextEntry bullets with a custom font?

I’m using a custom font in a UITextField, which has secureTextEntry turned on. When I’m typing in the cell, I see the bullets in my chosen font, but when the field loses focus, those bullets revert to the system standard font. If I tap the field again, they change back to my font, and so on.
Is there a way I can ensure that they continue to display the custom font’s bullets, even when the field is out of focus?
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.
For those having trouble with losing custom fonts when toggling secureTextEntry, I found a work-around (I'm using the iOS 8.4 SDK). I was trying to make a toggle for showing/hiding a password in a UITextField. Every time I'd toggle secureTextEntry = NO my custom font got borked, and only the last character showed the correct font. Something funky is definitely going on with this, but here's my solution:
-(void)showPassword {
[self.textField resignFirstResponder];
self.textField.secureTextEntry = NO;
}
First responder needs to be resigned for some reason. You don't seem to need to resign the first responder when setting secureTextEntry to YES, only when setting to NO.
The actual problem appears to be that the editing view (UITextField does not draw its own text while editing) uses bullets (U+2022) to draw redacted characters, while UITextField uses black circles (U+25CF). I suppose that in the default fonts, these characters look the same.
Here's an alternate workaround for anyone interested, which uses a custom text field subclass, but doesn't require juggling the text property or other special configuration. IMO, this keeps things relatively clean.
#interface MyTextField : UITextField
#end
#implementation MyTextField
- (void)drawTextInRect:(CGRect)rect
{
if (self.isSecureTextEntry)
{
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = self.textAlignment;
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:self.font forKey:NSFontAttributeName];
[attributes setValue:self.textColor forKey:NSForegroundColorAttributeName];
[attributes setValue:paragraphStyle forKey:NSParagraphStyleAttributeName];
CGSize textSize = [self.text sizeWithAttributes:attributes];
rect = CGRectInset(rect, 0, (CGRectGetHeight(rect) - textSize.height) * 0.5);
rect.origin.y = floorf(rect.origin.y);
NSMutableString *redactedText = [NSMutableString new];
while (redactedText.length < self.text.length)
{
[redactedText appendString:#"\u2022"];
}
[redactedText drawInRect:rect withAttributes:attributes];
}
else
{
[super drawTextInRect:rect];
}
}
#end
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.
I found a trick for this issue.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField tag]== TAG_PASS || [textField tag]== TAG_CPASS)
{
// To fix password dot size
if ([[textField text] isEqualToString:#"" ])
{
[textField setText:#" "];
[textField resignFirstResponder];
[textField becomeFirstResponder];
[textField setText:#""];
}
}
}
[passWordTextField resignFirstResponder];
passWordTextField.secureTextEntry = !passWordTextField.secureTextEntry;
[passWordTextField becomeFirstResponder];
This is the fastest way to solve this bug!
iOS is acting a bit strange when it comes to custom fonts. Try removing "Adjust to Fit" for that textfield. If that doesn't work, I'm guessing that what bothering you is the size increase of the font.
A simple solution for that would be:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField.secureTextEntry)
{
[textField setFont:[UIFont fontWithName:#"Helvetica-Bold" size:10.0]];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField.secureTextEntry)
{
[textField setFont:[UIFont fontWithName:#"Helvetica-Bold" size:10.0]];
}
}
You'll need to play with the size a bit in order for it to look like there is no size change when loosing focus on the UITextField.
If you have a major spacing problem between characters like in the edited question, the simplest (and a bit ugly) solution would be to create a Bullet image that matches the above size & spacing and matches the amount of characters entered by the user that will appear when the user leaves the UITextField.
A secureTextEntry text field can be avoided altogether:
NSString *pin = #"";
BOOL pasting = FALSE;
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(!pasting) {
pin = [pin stringByReplacingCharactersInRange:range withString:string];
// Bail out when deleting a character
if([string length] == 0) {
return YES;
}
pasting = TRUE;
[textField paste:#"●"];
return NO;
} else {
pasting = FALSE;
return YES;
}
}
I recommend to resignFirstResponder before you change scureTextEntry and then becomeFirstResponder again as it is posted here: https://stackoverflow.com/a/34777286/1151916
Swift 5 and iOS 14 is around but isSecureTextEntry set to true for custom font still displays the wrong size bullets, although the actual leading letter is of the correct size.
None of the solutions from stack overflow has worked for me except a hacky workaround of setting the font to the system font when password is in secure mode.
if textField.isSecureTextEntry {
self.textField.font = UIFont.systemFont(ofSize: 17)
} else {
self.textField.font = UIFont(name: "Roboto-Regular", size: 17)
}

Resources