Question

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:

  1. user taps on textfield
  2. user presses the number 1 key
  3. notification is sent that user pressed the number 1 key
  4. instead of the number 1 printed, the text will be replaced with the number 2.

this is a simple example.

Was it helpful?

Solution 2

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;
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top