5

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

用户可以在 UITextView 的任意位置输入单词,在中间或末尾或开头。当用户完成输入并按下空格并使用“来自的建议UIMenuController”进行任何更正时,我会认为它是一个词。

示例:用户在文本中间的某处输入“kimd” UITextView,他会弹出一个自动更正“种类”的弹出窗口。在他这样做之后,我想捕获“善良”并在我的应用程序中使用它。

我在互联网上进行了很多搜索,但找到了有关用户最终何时输入文本的解决方案。我还尝试检测一个空格,然后进行向后搜索,直到找到一些文本后的另一个空格,这样我就可以将它限定为一个单词。但我认为可能有更好的方法来做到这一点。

我在某处读到 iOS 缓存了我们在文本字段或文本视图中输入的最近文本。如果我能从顶部弹出,这就是我想要的。我只需要处理那个对象。

我非常感谢您的帮助。

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

谢谢。

4

4 回答 4

3
//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]; 
}
}
于 2013-01-10T15:05:34.697 回答
2

这是我建议做的事情,可能看起来有点老套,但它会很好用:

首先在 .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!
}

}
于 2012-12-29T19:36:53.433 回答
1

我使用此代码来获取 @-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);

        }
    }
}
于 2012-12-30T13:46:08.720 回答
1

的简单扩展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!
    }
}
于 2016-01-04T15:54:14.600 回答