在UITextView中获取当前键入的单词 [英] Get currently typed word in a UITextView

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

问题描述

我想在UITextView中获取当前键入的单词.可以在此处 UITEXTVIEW中找到获取完全键入的单词的方法:在uitextview中键入了最近的单词,但是,我想在键入单词时得到该单词(无论它在哪里键入,UITextView的开头,中间,结尾).

I want to get the currently typed word in a UITextView. A way to get a completely typed word can be found here UITEXTVIEW: Get the recent word typed in uitextview, however, I want to get the word while it is being typed (no matter where it is typed, beginning, middle, end of UITextView).

我想定义要键入的单词的是空格字符,后跟字母数字字符,或者如果当前位置(以某种方式用range.location找出)是UITextView的开头,那么应该考虑后续内容以及单词.当另一个空格字符跟在后面时,单词输入完毕.

I guess what defines a word being typed is a whitespace character followed by alphanumeric characters, or if the current location (somehow to figure out with range.location) is the beginning of the UITextView then whatever follows up should be considered as word as well. A word is finished being typed when another whitespace character follows.

我尝试过:

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text`

但是我在找到正确的范围时遇到问题.

but I have a problem finding the correct range.

简而言之:我需要在UITextView中找到子字符串,其中子字符串是当前键入的单词.

In short again: I need to find the substring in a UITextView, where substring is the currently typed word.

编辑:因为出现了问题.我正在使用NSLayoutManager并将NSTextContainer绑定到它,然后将它作为layoutManager传递给NSTextStorage.

EDIT: Because the question came up. I'm using NSLayoutManager and bind a NSTextContainer to it, which then is passed as layoutManager to a NSTextStorage.

EDIT2 :(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text的主要问题在于range.location与指标不同,但总是少1.如果光标位于位置1,则输入一个字母后,range.location将返回0.另外,UITextViewtext属性似乎也被关闭了1.当文本为foobar并且输出UITextView.text时,我得到的是fooba.因此range.location+1返回异常Range {0, 1} out of bounds; string length 0'

EDIT2: The main problem with (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text is that the range.location is not the same as the indicator is, but always 1 less. If the cursor is at position 1, after typing one letter, range.location would return 0. Any way around this? In addition the text attribute of UITextView seems to be off by 1 as well. When the text is foobar and I output the UITextView.text then I get fooba instead. Therefore range.location+1 returns the exception Range {0, 1} out of bounds; string length 0'

EDIT3 : 也许用NSTextStorage而不是UITextView更容易得到我想要的结果?

EDIT3: Maybe it is easier to get my wanted result with the NSTextStorage instead of UITextView?

推荐答案

UITextView委托方法:-textView:textView shouldChangeTextInRange:range replacementText:text实际执行的操作是询问是否应在.

The UITextView delegate method : -textView:textView shouldChangeTextInRange:range replacementText:text what actaully does is that it asks whether the specified text should be replaced in the text view in the specified range of textView.text .

每次在将字符更新到文本视图之前键入一个字符时,都会调用此方法.这就是为什么在textView中键入第一个字符时将range.location设置为0的原因.

This method will be invoked each time when we type a charactor before updating that to the text view. That is why you are getting the range.location as 0, when you type the very first character in the textView.

仅当此方法的返回为true时,textView才会使用我们在textView中键入的内容进行更新.

Only if the return of this method is true, the textView is getting updated with what we have typed in the textView.

这是苹果提供的-textView:textView shouldChangeTextInRange:range replacementText:text方法的参数的定义:

This is the definition of the parameters of the -textView:textView shouldChangeTextInRange:range replacementText:text method as provided by apple:

范围:-当前的选择范围.如果范围的长度为0,则范围反映当前的插入点.如果用户按下Delete键,则范围的长度为1,并且一个空字符串对象替换了该单个字符.
文本:-要插入的文本.

range :- The current selection range. If the length of the range is 0, range reflects the current insertion point. If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character.
text :- The text to insert.

因此,该方法的说明和您的要求可以满足,如下所示:

So this is what the explanation for the method and your requirement can meet like as follows:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
        //Un-commend this check below :If you want to detect the word only while new line or white space charactor input 
        //if ([text isEqualToString:@" "] || [text isEqualToString:@"\n"])
        //{
            // Getting the textView text upto the current editing location
            NSString * stringToRange = [textView.text substringWithRange:NSMakeRange(0,range.location)];

            // Appending the currently typed charactor
            stringToRange = [stringToRange stringByAppendingString:text];

            // Processing the last typed word 
            NSArray *wordArray       = [stringToRange componentsSeparatedByString:@" "];
            NSString * wordTyped     = [wordArray lastObject];

            // wordTyped will give you the last typed object
            NSLog(@"\nWordTyped :  %@",wordTyped);
        //}
        return YES;
}

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

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