iOS从UITextView中删除单词 [英] iOS remove word from UITextView
本文介绍了iOS从UITextView中删除单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在UITextView中有一个字符串:
Suppose, I have a string in a UITextView that is :
NSString *str = @"Hello world. What @are you @doing ?"
当我点击文本时,我可以逐个删除字符。但我想要的是,如果任何单词以 @
开头(例如:@are),那么当我点击该单词并按退格键时,整个单词(即@are)应该被删除而不是字符。有可能当我点击任何带有前缀'@'的单词(如:@are)时,它会突出显示并按退格键会删除该单词吗?
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 ?
我怎么能这样做?
推荐答案
好的我有解决方案和工作:)
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
这篇关于iOS从UITextView中删除单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文