UITEXTVIEW: Get the recent word typed in uitextview

人走茶凉 提交于 2019-12-05 13:34:21
//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]; 
}
}

Here is what I would suggest doing, might seem a little hacky but it would work just fine:

First in .h conform to the UITextViewDelegate and set your text view's delegate to self like this:

myTextView.delegate = self;

and use this code:

- (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!
}

}

I use this code to get the word behind the @-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);

        }
    }
}

Simple extension for 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!
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!