确认 UITextview 自动完成
这似乎不可能,但也许其他人也遇到过同样的问题。
我是否可以以编程方式接受自动完成,或者以某种方式获取弹出的建议单词?我的问题是我正在捕获返回/退格键击,然后将焦点移动到另一个文本视图。当按下回车/退格键时,文本视图将忽略自动建议的单词。似乎只能通过点击空格/点来接受自动完成(并返回新行)。使用此代码:
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
NSRange textViewRange = [textView selectedRange];
// Handle newrow and backspace.
if(([text length] == 0) && (textViewRange.location== 0) && textViewRange.length==0){
// BACKSPACE KEYSTROKE
[delegate doSomethingWhenBackspace];
return NO;
}else if ([text isEqualToString:@"\n"]){
// RETURN KEYSTROKE
[delegate doSomethingWhenReturn];
return NO;
}
return YES;
}
我尝试在按下返回键时以编程方式添加“空格”,但这也忽略了自动完成的单词。
else if ([text isEqualToString:@"\n"]){
// Tryin to accept autocomplete with no result.
textview.text = [textview.text stringByAppendingString:@" "];
// RETURN KEYSTROKE
[delegate doSomethingWhenReturn];
return NO;
}
有什么建议吗?
This seems impossible, but maybe someone else has had the same problem.
Is it possible for me to accept an autocomplete programmatically, or in some way get the suggested word that pops up? My problem is that I'm capturing the return/backspace keystroke and then move focus to another textview. When enter/backspace is hit, the textview will ignore the auto-suggested word. It seems that it is only possible to accept an autocompletion by hit space/dot (and return for new row). With this code:
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text {
NSRange textViewRange = [textView selectedRange];
// Handle newrow and backspace.
if(([text length] == 0) && (textViewRange.location== 0) && textViewRange.length==0){
// BACKSPACE KEYSTROKE
[delegate doSomethingWhenBackspace];
return NO;
}else if ([text isEqualToString:@"\n"]){
// RETURN KEYSTROKE
[delegate doSomethingWhenReturn];
return NO;
}
return YES;
}
I tried to programmatically add "space" when the return key is hit but that also ignores the auto-completed word.
else if ([text isEqualToString:@"\n"]){
// Tryin to accept autocomplete with no result.
textview.text = [textview.text stringByAppendingString:@" "];
// RETURN KEYSTROKE
[delegate doSomethingWhenReturn];
return NO;
}
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在需要接受自动完成结果的文本视图或文本字段上调用
-resignFirstResponder
(即[textView resignFirstResponder]
):UIKit将更改.text
属性以包含自动更正的文本。如果您希望在第一个视图放弃第一响应者后保持键盘处于打开状态,请使用
[anotherTextViewBecomeFirstResponder]
将firstResponder
职责传递到下一个文本输入视图。Call
-resignFirstResponder
(i.e.[textView resignFirstResponder]
) on the text view or text field which needs to accept autocomplete results: UIKit will change the.text
property to include the autocorrected text.If you want to keep the keyboard up after your first view resigns first responder, pass the
firstResponder
responsibility onto your next text input view with[anotherTextView becomeFirstResponder]
.对于退格键和空格键,您可以使用此条件
For backspace and space u can use this condition
您正在寻找
[textView unmarkText]
。You're looking for
[textView unmarkText]
.我遇到了一个非常类似的问题,我正在制作一个应用程序,必须读取文本视图中的每个字母,并且在自动完成插入单词时遇到问题,因为它将它保存为一个字母。
您可以将每个字符添加到数组中,然后检查是否有字符长度超过 1 个字符串。或者,您可以添加放入数组中的每个字符,然后运行类似
单独添加每个字符的操作,通过比较两个数组,您可以确定是否使用了自动更正以及使用了哪些单词。
希望这会有所帮助。
I've had a very similar problem, I was making an app that had to read every letter in a text view and I has issues when Autocomplete inserted words because it was saving it as if it was one letter.
you could add each character to an array and then check to see if any are over 1 string in length. Or you could add each character that is put in into an array and then run something like
to add each character individually, by comparing the two arrays you could determine if autocorrect has been used and with what word/s.
Hope this will help.