Restrict deletion on UITextField - ios

I've a UITextField with text like, +91- (that's country caller code of India). Now when my user input his number into that textfield, it would look like this, +91-1234567890 that's good, now when he tap (x) delete key from keyboard, I want to restrict deletion, its only possible up to, 1 (first digit of his mobile number), at any case, he should not be able to delete +91-. I'm able to do it with - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; delegate, like this,
1) First way:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([string isEqualToString:#""]) { //detect back space
if([textField.text hasSuffix:#"-"]) { //has suffix `-`
return NO;
}
}
}
2) Second way:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//if text length is length of caller code and detect back space
if(textField.text.length<=4 && [string isEqualToString:#""]) {
return NO;
}
return YES;
}
In both the ways, I'm getting what I want, but not sure its proper or not? Any more smoother way?

why you not try just like the simple method add the one more UIView in prefix of the UItextfield

You can simply show a non-editable UILabel before the UITextField where the user actually enters the number. When you get the input from the user and want to process it, prefix "+91-" before the user input string

Related

iOS UItextfield reload after key hit

I am making a simple application that has a TextField and a TextView
Whatever we write in textfield will get updated in textview (Both are in a single View, and it is the Root view).
the textfield will get updated every time i hit any key in the keyboard.
I know that the delegate method "textfielddidEditing" helps , but it only helps when we click on the field or click back. I want the method that invokes every time when i hit anything in the keyboard.
Any help is appreciated.
I'm still learning iOS
Please use below textfield delegate method. Its easy and simple to use with all appen and delete mechanism. You can delete multiple characters too by selection. Just store newString value to your textView
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSMutableString *newString = [NSMutableString stringWithString:textField.text];
if (range.length > 0) {
[newString replaceCharactersInRange:range withString:string];
}
else {
[newString appendString:string];
}
NSLog(#"%#",newString);
return YES;
}
You can use UITextField Delegate method. It invokes every time when you hit anything in the keyboard. Write your code as per your requirement..
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length>0)
{
self.yourtextview.text=textField.text;
}
return YES;
}
Hope it helps you..
The above code mentioned by Vidhyanad900 will work. But its if would fail in case a user deletes the text. (Hits backspace or selects complete text and deletes it)
Hence Modifying the code
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int iNewLength = [textField length];
if([text isEqualToString:#""]) //If text is deleted
{
if([textField selectedTextRange]) //If Text is Seleceted And Deleted
iNewLength = [textField length] - [textField selectedRange].length;
else
iNewLength = [textField length] - 1;
}
if (iNewLength > 0)
{
self.yourtextview.text=textField.text;
}
return YES;
}

How can I recognize keyboard presses in ios?

I have looked all over for an answer to this but essentially what I am trying to do is when a person pressed the colon key on their iphones keyboard I want to be notified and perform a certain action. I hope this makes sense. If you do offer an answer keep in mind I am a relatively new IOS developer :)
Thanks!
edit: Incase my above statement didn't quite make sense this is what will happen ideally:
user taps on textfield
user presses the number 1 key
notification is sent that user pressed the number 1 key
instead of the number 1 printed, the text will be replaced with the number 2.
this is a simple example.
Here's an example of a delegate method for a UITextField where if the user tries to enter an uppercase character it will appear as a lowercase character instead:
-(BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSString* lc = [string lowercaseString];
if ([string isEqualToString:lc])
return YES;
textField.text =
[textField.text stringByReplacingCharactersInRange:range
withString:lc];
return NO;
}
You should be able to do something similar for your particular use case.
As mentioned before, use this callback and change in there:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//check here if the new character is the one you are looking for
if ([string isEqualToString:#"a"])
{
//create a new string with the character you want to use instead
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:#"A"];
//set it as the text for your text field
[textField setText:newText];
return NO;
}
return YES;
}

method UItextfield - after I type in a UITextField

I'm looking for a method of UITextField, which makes a thing after I type in a UITextField.
I have a method to conduct a calculation I use a button, but I do not want to use, I want you to run this method after I type numbers in UITextField.
any tips for which method to use?
thanks for the help.
Implement the UITextFieldFieldDelegate protocol
Set the delegate of your textfields to your view controller
self.textField.delegate = self;
Then Implement the shouldChangeCharactersInRange method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
[self doCalculations];
return NO;
}
You can use - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string to get the typed text straight away. Or you can use - (BOOL)textFieldShouldReturn:(UITextField *)textField to capture when the user hits return and just read the text out of the text field at that time. Both of these methods are in the UITextFieldDelegate protocol, the docs for which can be found here.
EDIT:
Alternatively you could use [textField addTarget:self action:#selector(textChanged:) forControlEvents:UIControlEventEditingChanged]; and implement the textChanged method to capture the EditingChanged event.
//in your ViewContoller.h
//Implement UITextFieldFieldDelegate protocol
//In your ViewContoller.m
//where you are creating your textfield.
textField.delegate = self;
// There are 2 delegate method that you can implement
// this method will be called whwn ever you enter a a letter.
// say you enter 'abcd' then this method will be called 4 times.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//do your calculation if required
return YES;
}
//this method will be called when the textField will end editing, i.e when the keyboard resigns of the control goes to some other textField/textView
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
Based on your requirement, you can either do your calculation in the 1st or the 2nd method.
Yes you can do so by using this code :-
This code Asks the delegate if the specified text should be changed.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self yourMethodName]; //The method which you want to call.
return YES;
}
and call the UITextFieldField Delegate.For better understanding of this you can refer this:-
https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString:
Hope this will help you out.

Adding text to UITextField you can't delete

What I would like to do is to add some text at the beginning of UITextField that cannot be deleted. I add this text when the user taps on UITextField like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.text.length == 0) {
textField.text = #"myText";
}
return YES;
}
How do I prevent the user from deleting this text in a normal way? (without adding a separate UILabel on top of the TextField, for example).
Thanks in advance!
You can implement this code in shouldChangeCharactersInRange delegate method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length == 1 && [string isEqualToString:#""]) {//When detect backspace when have one character.
textField.text = #"myText";
}
return YES;
}
Checkout shouldChangeCharactersInRange in the UITextFieldProtocolReference
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
the text field calls this method whenever the user types a new
character in the text field or deletes an existing character.
So, you have control on whether text actually gets changed so you can insert your custom logic in that delegate callback enforcing whatever rules about what can change.
Use this
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([[textField text] length]==LENGTH_OF_TEXT && [string isEqualToString:#""])
{
return NO;
}
else
return YES;
}
Hope it helps you.

Notifications of backspace key iOS

Is there a way to get notifications of when the user presses the backspace key on a UITextField, even if the field is empty?
I would like to be able to trigger some code when the user backspaces on an empty field.
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
This is how you get notified on every key the user presses. Then you have to figure out (i think in the notification is a property for it) which key was pressed.
Or you can use the textfield delegate function
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
To figure out what key was pressed
I can think of a technique to get you what you want but its going to take a bit of code. In essence, always keep a single Unicode "space" character at the front of the string. I don't have my book with me, but there is a really thin space character you can use.
You will put most of your code here:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
when the field begins editing, prefix any string there with the space
when the user appends text as seen in shouldChangeCharactersInRange, append it.
if the user tries to delete the final last character, or a range that contains it, then return NO, make the change, then add back the leading space char.
when the textField is finished editing, remove the leading space
Since you need to detect not when text was deleted but when the backspace key is pressed, you need to do quite a bit more than implement UITextFieldDelegate.
Read this blog post about how UITextField forwards all the UIKeyInput methods to a private class UIFieldEditor. The writer dynamically subclasses UIFieldEditor at runtime in order to detect these events.
Hope this points you in the right direction!
This will detect backspace. Now you can do whatever when backspace is pressed.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField==txtMobileNo)
{
const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
int isBackSpace = strcmp(_char, "\b");
if (isBackSpace == -8) {
NSLog(#"isBackSpace");
if (textField.text.length == 9)
{
}
return YES; // is backspace
}
else if (textField.text.length == 10) {
return YES;
}
}
return NO;
}

Resources