0

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.

3
  • Is this for a UITextField or a UITextView? Commented Mar 20, 2014 at 16:47
  • 1
    Have you looked at the docs for UITextFieldDelegate? Commented Mar 20, 2014 at 16:53
  • So it would be the textfield delegate rather then the keyboard? I need the notification before the enter/return key is pressed. Commented Mar 20, 2014 at 16:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That is pretty much what I am looking for as far as changing out the letters so thank you! Although it appears as though what will happen is once the user enters the full sentence it would then change around what is typed. How would I receive a notification from the keyboard so that for example: when user types a, it changes to A, user types p, changes to P etc.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.