Search code examples
iphoneiosuitextfielduitextfielddelegate

UITextField highlight text when user taps space


I have a UITextField which i let user write tags as we do in stackoverflow. What i really want is exactly what stackoverflow does,with a slight difference which is to detect user's touch space button in keyboard.Well,stackoverflow searches instantaneously and display results.Instead i want user to tap space button. Do u guys have any idea,any tutorial that i can benefit from ? I'm sure i'm going to use UITextFieldDelegate method which is


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string


But i don't know how to highlight the words before user starts to tap another word .


Solution

  • To get text highlighted u will have to use NSAttributestring and for that u will have use UITextView that supports NSAttributestring.

    Refer EGOTextView link that supports NSAttributestring

    Do like this to detect space in UITextField like this:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
         NSString *lastCharacter = [string substringWithRange:NSMakeRange([string length]-1, 1)];
         if([lastCharacter isEqualToString:@" "])
         {
             //space button pressed
         }
         else
         { 
             // continue
         }
    }