Suppose, I have a string in a UITextView that is :
NSString *str = @"Hello world. What @are you @doing ?"
When I tap on the text, I can delete the character by character. But what I want is if any word starts with @
(like: @are) then when I tap on that word and press backspace the entire word (i.e, @are)should be deleted instead of a character. Is it possible that when I tap on any word that has a prefix '@' (like: @are) it will be highlighted and press backspace will delete that word ?
How can I do that?
Ok i have solution for that and Working :)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([string isEqualToString:@""]) {
UITextRange* selectedRange = [textField selectedTextRange];
NSInteger cursorOffset = [textField offsetFromPosition:0 toPosition:selectedRange.start];
NSString* text = textField.text;
NSString* substring = [text substringToIndex:cursorOffset];
NSString* lastWord = [[substring componentsSeparatedByString:@" "] lastObject];
if ([lastWord hasPrefix:@"@"]) {
// Delete word
textField.text = [[self.textField text] stringByReplacingOccurrencesOfString:lastWord withString:@""];
return NO;
}
}
return YES;
}// return
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With