UITEXTVIEW:获取在uitextview中键入的最近单词 [英] UITEXTVIEW: Get the recent word typed in uitextview

查看:73
本文介绍了UITEXTVIEW:获取在uitextview中键入的最近单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the most recent word entered by the user from the UITextView.

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

The user can enter a word anywhere in the UITextView, in the middle or in the end or in the beginning. I would consider it a word when the user finishes typing it and presses a space and does any corrections using the "Suggestions from the UIMenuController".

示例:用户在文本中间某处的UITextView中键入"kimd",他会弹出一个用于自动更正种类"的弹出窗口.在他完成之后,我想捕获种类"并在我的应用程序中使用它.

Example: User types in "kimd" in the UITextView somewhere in the middle of text, he gets a popup for autocorrection "kind" which he does. After he does that, I want to capture "kind" and use it in my application.

我在互联网上进行了很多搜索,但是找到了解决方案,这些解决方案可以讨论用户最后输入文本的时间.我还尝试了检测一个空格,然后进行向后搜索,直到找到一些文本后找到另一个空格,以便我可以将其限定为一个单词.但是我认为可能会有更好的方法.

I searched a lot on the internet but found solutions that talk about when the user enters text in the end. I also tried detecting a space and then doing a backward search until another space after some text is found, so that i can qualify it as a word. But I think there may be better ways to do this.

我读过某个地方,iOS缓存了我们在文本字段或文本视图中输入的最新文本.如果我可以跳出最上面的一个,那就是我想要的.我只需要处理该对象即可.

I have read somewhere that iOS caches the recent text that we enter in a text field or text view. If I can pop off the top one , that's all I want. I just need handle to that object.

我非常感谢您的帮助.

注意:用户可以在UItextview中的任何位置输入文本.我需要最近输入的单词

Note: The user can enter text anywhere in UItextview. I need the most recent entered word

谢谢.

推荐答案

//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]; 
}
}

这篇关于UITEXTVIEW:获取在uitextview中键入的最近单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆