UITEXTVIEW:获取在uitextview中input的最近单词

我想从UITextView获取用户input的最新单词。

用户可以在UITextView的任何位置input一个单词,也可以在中间或末尾或开头input单词。 当用户input完成并按下一个空格并使用“来自UIMenuControllerbuild议”进行修改时,我会认为这是一个单词。

例如:用户在UITextView的文本中间的某处input“kimd”,他得到一个自动更正的popup窗口“kind”。 他这样做后,我想捕捉“善良”,并在我的应用程序中使用它。

我在互联网上search了很多东西,但是发现了解决scheme,最终用户input文本的时候就会谈论这个问题。 我也尝试过检测一个空间,然后做一个向后的search,直到find一些文本之后的另一个空间,这样我就可以将它定义为一个单词。 但我认为可能有更好的方法来做到这一点。

我已经在某处读过iOScaching我们在文本字段或文本视图中input的最近文本。 如果我可以从最高的那个popup,那就是我想要的。 我只需要处理该对象。

我真的很感谢帮助。

注意:用户可以在UItextview的任何地方input文字。 我需要最近input的单词

谢谢。

 //This method looks for the recent string entered by user and then takes appropriate action. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { //Look for Space or any specific string such as a space if ([text isEqualToString:@" "]) { NSMutableCharacterSet *workingSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy]; NSRange newRange = [self.myTextView.text rangeOfCharacterFromSet:workingSet options:NSBackwardsSearch range:NSMakeRange(0, (currentLocation - 1))]; //The below code could be done in a better way... UITextPosition *beginning = myTextView.beginningOfDocument; UITextPosition *start = [myTextView positionFromPosition:beginning offset:currentLocation]; UITextPosition *end = [myTextView positionFromPosition:beginning offset:newRangeLocation+1]; UITextRange *textRange = [myTextView textRangeFromPosition:end toPosition:start]; NSString* str = [self.myTextView textInRange:textRange]; } } 

这是我会build议做的,可能看起来有点黑客,但它会工作得很好:

首先在.h符合UITextViewDelegate并设置您的文本视图的delegateself像这样:

 myTextView.delegate = self; 

并使用此代码:

 - (void)textViewDidChange:(UITextView *)textView { // Delegate method called when any text is modified if ([textView.text substringFromIndex: [textView.text length] - 1]) { // Gets last character of the text view's text NSArray *allWords = [[textView text] componentsSeparatedByString: @" "]; // Gets the text view's text and fills an array with all strings seperated by a space in text view's text, basically all the words NSString *mostRecentWord = [allWords lastObject]; // The most recent word! } } 

我使用这个代码来获取@ -sign后面的单词:

 - (void)textViewDidChange:(UITextView *)textView { NSRange rangeOfLastInsertedCharacter = textView.selectedRange; rangeOfLastInsertedCharacter.location = MAX(rangeOfLastInsertedCharacter.location - 1,0); rangeOfLastInsertedCharacter.length = 1; NSString *lastInsertedSubstring; NSString *mentionSubString; if (![textView.text isEqualToString:@""]) { lastInsertedSubstring = [textView.text substringWithRange:rangeOfLastInsertedCharacter]; if (self.startOfMention > 0 || self.startOfHashtag > 0) { if ([lastInsertedSubstring isEqualToString:@" "] || (self.startOfMention > textView.selectedRange.location || self.startOfHashtag > textView.selectedRange.location)) { self.startOfMention = 0; self.lenthOfMentionSubstring = 0; } } if (self.startOfMention > 0) { self.lenthOfMentionSubstring = textView.selectedRange.location - self.startOfMention; NSRange rangeOfMentionSubstring = {self.startOfMention, textView.selectedRange.location - self.startOfMention}; mentionSubString = [textView.text substringWithRange:rangeOfMentionSubstring]; dhDebug(@"mentionSubString: %@", mentionSubString); UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil); } } } 

UITextView简单扩展:

 extension UITextView { func editedWord() -> String { let cursorPosition = selectedRange.location let separationCharacters = NSCharacterSet(charactersInString: " ") let beginRange = Range(start: text.startIndex.advancedBy(0), end: text.startIndex.advancedBy(cursorPosition)) let endRange = Range(start: text.startIndex.advancedBy(cursorPosition), end: text.startIndex.advancedBy(text.characters.count)) let beginPhrase = text.substringWithRange(beginRange) let endPhrase = text.substringWithRange(endRange) let beginWords = beginPhrase.componentsSeparatedByCharactersInSet(separationCharacters) let endWords = endPhrase.componentsSeparatedByCharactersInSet(separationCharacters) return beginWords.last! + endWords.first! } }