How do I stop UITextField Secure text showing characters - ios

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.

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

when textfield:shouldChangeCharactersInRange:replacementString: return NO on iPad iOS 9, it will crash

I want to limit the length of the text of a UITextField, then I implement the UITextFieldDelegate method:
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string {
return textField.text.length + string.length < 20;}
On iPad and iOS 9, the keyboard will show three buttons on the left top corner of the keyboard. When the length of the text reaches the max number, the method will return NO. Firstly I tapped button 3, then button 1. Why crashed my app?
My GUI image and buttons
Try this,
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ( textField.text.length + string.length > 20) {
return NO;
}
if (range.location == 20) {
return NO;
}
return YES;
}
because when you are pressing undo or redo button then it will call substringWithRange and you are getting invalid range that's why your app is crashing!
you can check it better by putting breakpoints in your shouldChangeCharactersInRange method. check value of range and string every time!

Stop UITextField Secure Text From Clearing Existing Text

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.

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