使用UITextView的自动补全应该更改范围内的文本 [英] Autocompletion using UITextView should change text in range

查看:70
本文介绍了使用UITextView的自动补全应该更改范围内的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遵循了Ray Wenderlich的关于文本自动完成的教程: http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values



效果很好,但是它仅允许搜索textView中包含的字符串。我需要它在同一视图中搜索多个字符串。例如:

 您好@ user,@ steve 


应搜索两个出现的@。这是原始代码:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string {
autocompleteTableView.hidden = NO;

NSString * substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
返回是;
}

这是我的:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
replaceString:(NSString *)string {

NSArray *项目= [textView.text componentsSeparatedByString:@ @];

if([项目计数]> 0&& [[[项目lastObject]长度]){
NSString * substring = [NSString stringWithString:[项目lastObject]]];
[self searchAutocompleteEntriesWithSubstring:substring];

}
返回是;
}

一切正常,但似乎是后面的角色。因此,键入 J将什么都不会产生,但是 Jo将返回 J的结果。我认为它与以下内容有关:

  [substring stringByReplacingCharactersInRange:range withString:string] 

但是我尝试使用NSRange超出范围时都会崩溃。

解决方案

首先,我们是在谈论文本字段还是文本视图?他们是不同的!您的代码同时引用了两者。我将只使用 textField ,因为那是方法的名称。



在您收到 textField:shouldChangeCharactersInRange:replacementString: textField.text 尚未更改为包含替换字符串。因此,当用户键入 J时, textField.text 尚不包含 J



Ray的方法通过执行替换来处理此问题。当您尝试进行替换时,它会失败,因为您的子字符串变量不包含 textField.text 的副本。您的子字符串仅包含 textField.text 的一部分。这就是为什么出现越界异常的原因-您的范围超出了范围,因为 substring textField.text



所以也许您应该在分割字符串之前执行替换。试试这个:

 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replaceString:(NSString *)字符串{
NSString * changedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *项目= [changedText componentsSeparatedByString:@ @];

if([项目计数]> 0&& [[[项目lastObject]长度]){
NSString * substring = [NSString stringWithString:[项目lastObject]]];
[self searchAutocompleteEntriesWithSubstring:substring];

}
返回是;

}


I followed Ray Wenderlich's tutorial on text autocompletion here: http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values

And it works great, but it only allows searching for a string that is contained in the textView. I need it to search for multiple strings in the same view. For example:

Hi @user, how's @steve

Should search for both occurrences of the @. Here is the original code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string {
 autocompleteTableView.hidden = NO;

    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

And here is mine:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range 
replacementString:(NSString *)string {

NSArray* items = [textView.text componentsSeparatedByString:@"@"];

if([items count] > 0 && [[items lastObject] length]){
    NSString *substring = [NSString stringWithString:[items lastObject]];
    [self searchAutocompleteEntriesWithSubstring:substring];

    }
return YES;
}

And everything works, but it seems to be a character behind. So typing "J" would result in nothing, but "Jo" would return results for "J". I'm thinking it has to do with:

 [substring stringByReplacingCharactersInRange:range withString:string]

But anything I try crashes with an NSRange out of bounds.

解决方案

First of all, are we talking about a text field or a text view? They are different! Your code refers to both. I will just go with textField since that's in the name of the method.

At the time you receive textField:shouldChangeCharactersInRange:replacementString:, textField.text has not been changed to contain the replacement string. So when the user types "J", textField.text doesn't contain the J yet.

Ray's method handles this by performing the substitution. When you try to do the substitution, it fails because your substring variable doesn't contain a copy of textField.text. Your substring only contains a part of textField.text. That's why you get an out-of-bounds exception - your range is out of bounds because substring is shorter than textField.text.

So perhaps you should perform the replacement before you split the string. Try this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *changedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray* items = [changedText componentsSeparatedByString:@"@"];

    if([items count] > 0 && [[items lastObject] length]){
        NSString *substring = [NSString stringWithString:[items lastObject]];
        [self searchAutocompleteEntriesWithSubstring:substring];

    }
    return YES;

}

这篇关于使用UITextView的自动补全应该更改范围内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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