Stop UITextField Secure Text From Clearing Existing Text - ios

I have a UITextField that is a password field declared by using
passwordField.secureTextEntry = YES;
My issue is that when I type half my password, click on a different component, and come back to the passwordField and start typing again, iOS clears the existing text.
Is there a way to prevent this from happening or turn it off?
-Henry

Simplest way to do this is to implement delegate for UITextField with a code similar to this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementString {
if (textField.text) {
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:replacementString];
} else {
textField.text = replacementString;
}
return NO;
}

By default, no, you can't do it for secure text fields. You can work around it though.
Implement the delegate method textField:shouldChangeCharactersInRange:replacementString: and use the values passed to decide what to do.
i.e. if the range is the entire string and the replacement is an empty string, don't allow the change. The new text might be provided as the newly typed character, you'll need to check what parameters the method actually receives.

Well I have the same situation when I was changing keyboard type to make my password combo of 4(digits)+1(alpha), to stop clearing secureTextEntry,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
textField.keyboardType = UIKeyboardTypePhonePad;
[textField reloadInputViews];
return YES;
}
It reloads UITextField but not its text, so without clearing text it changed the keyboard from alpha to numeric vice versa. You can use it for your own functionality.

Related

objective c move cursor back after textField manipulation

I have a UITextField for the input of multiple twitter hashtags, each separated by a space.
Using this code, when you press the space bar a hash # is automatically added to speed up hashtag input.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#" "])
{
NSString *text = _hashtagTextView.text;
text = [text stringByAppendingString:#" #"];
_hashtagTextView.text = text;
}
return YES;
}
}
The problem is that this code adds a space after the # and so you need to press backspace to make the hashtag, I’m not sure why.
How can I programatically move the cursor back 1 character?
Take a look on your code one more time: text field asking for your permission to change some text (add to end in particular). If new fragment is whitespace, your appending to your text ‘ #’, and granting permission to add whitespace. Perahaps you want to forbide adding whitespace If yor had inserted ‘ #’ already

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 *

How do I stop UITextField Secure text showing characters

I've enabled Secure text for my UITextField. At runtime when I type some text in I initially see the character before it turns to a bullet point. Is there a way to stop it showing the characters and having it as a bullet point as I type.
Is there something I need to add to
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Has anyone got an example?
I dont know its good practice or not but following trick did it for me
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *editedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
textField.text = editedString;
return NO;
}
That is the default behaviour of the UITextField. So there's nothing much you can do about it. You can play with some kind of encryption on the coding side but on the view side, this behaviour will prevail.

iOS UITextField changes position when text is changed

I use this to center a text field over something relevant:
[textField setCenter:[someObject center]];
[textField becomeFirstResponder];
This looks great, nice and centered over the object, ready to accept text. I'd like the text formated a certain way, so in the editing changed handler, I revise the text and set to the new text:
[textField setText:newText];
When I do this, the text field will jump to its the old position, from before it was centered. I'd like the text field to stay centered over the object.
My experience is that there is likely a better way to do this, i.e. move the text field and dynamically change its contents, without have to work around various quirks. Any advice?
For Formatting the UITextField while editing you can try out the following code.
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if([string isEqualToString:#"4"]){
UITextRange *range=textField.selectedTextRange;
[textField replaceRange:range withText:#"Hi"];
return NO;
}
return YES;
}
I have kept UITextfield with centre alignment. When it detects "4" at the start,centre or any where, it will replace it with "Hi",while maintaining it's centre align behaviour.
In addition to the code above, used the following to replace the existing text:
UITextPosition *p0 = [textField beginningOfDocument];
UITextPosition *p1 = [textField positionFromPosition:p0 offset:[[textField text] length]];
UITextRange *complete = [textField textRangeFromPosition:p0 toPosition:p1];
[textField replaceRange:complete withText:newText];
Ghastly solution for what should be an easy task!

How to make both UITextFields content equal at the same time?

I have some UITextFields and one button, Can anyone tell me how to make both textfields (first Street Address field and Street Address field below Same As Above button) content equal at the same time if my button (Same As Above) is checked (I m using a boolean variable isSameAsAboveChecked on button click and making value Yes and No on respective actions).
Means If Same As Above button is checked, and i m writing something in First Street Address textfield then at the same time content of Second Street Address text field should start changing. and same for all the other fields.
Thnaks in advance.
Set both textfield.delegate =self
I just tried with :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
self.yourSecondField.text=self.yourFirstField.text;
return YES;
}
Issue: If First field has been entered as say "XYZA" , second field has just "XYZ", maybe someone can edit this answer or give a better one.
Use this delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range
withString:string];
text2Field.text = toBeString;
return YES;
}
In it first set the entered text to other textField and then return YES
Dont forget to return yes,
for this functionality use this code
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField == street_Address) {
if (check_box) {
street_address.text=street_Address.text;
}
else
[street_Address resignFirstResponder ];}
similarly u apply for rest of other fields. First u check ur check box is enable or not. if enable then pass the value to below text fileds otherwise resign current text field.

Resources