UITEXTVIEW: Get the last word entered in uitextview

I want to get the last word entered by the user from a UITextView.

The user can enter the word anywhere in the UITextView, in the middle or at the end or at the beginning. I would think that this word when the user finishes typing it and presses the space bar and makes any corrections using the "Suggestions from UIMenuController ".

Example. The user-defined types in "kimd" in a UITextView somewhere in the middle of the text, it gets a popup for auto-correction of the "kind" that it does. After that, I want to capture the “view” and use it in my application.

I searched a lot on the Internet, but found solutions that talk about when the user enters text at the end. I also tried to find a space and then do a reverse search until the next space after any text was found so that I could qualify it as a word. But I think there may be better ways to do this.

I read somewhere that iOS caches recent text that we enter in a text box or text view. If I can pull out the top, then all I want. I just need to process this object.

I am very grateful for the help.

Note. The user can enter text anywhere in the UItextview. I need the last word entered

Thanks.

+4
source share
4 answers
 //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]; } } 
+3
source

Here's what I propose to do, it may seem a bit hacky, but everything will work fine:

First in .h there is a UITextViewDelegate and set the following for your text delegate view to self :

 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 text NSArray *allWords = [[textView text] componentsSeparatedByString: @" "]; // Gets the text view text and fills an array with all strings seperated by a space in text view text, basically all the words NSString *mostRecentWord = [allWords lastObject]; // The most recent word! } } 
+2
source

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

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! } } 
0
source

All Articles