使用UITextView的自动补全应该更改范围内的文本 [英] Autocompletion using UITextView should change text in range
问题描述
我在这里遵循了Ray Wenderlich的关于文本自动完成的教程: http://www.raywenderlich.com/336/how-to-auto-complete-with-custom-values
效果很好,但是它仅允许搜索textView中包含的字符串。我需要它在同一视图中搜索多个字符串。例如:
您好@ user,@ steve
$ p $怎么样? p>
应搜索两个出现的@。这是原始代码:
-(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 $ c $短c>。
所以也许您应该在分割字符串之前执行替换。试试这个:
-(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 theJ
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 oftextField.text
. Yoursubstring
only contains a part oftextField.text
. That's why you get an out-of-bounds exception - your range is out of bounds becausesubstring
is shorter thantextField.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屋!