iPhone设置UITextView委托破坏自动完成

发布于 2024-08-26 09:58:25 字数 557 浏览 16 评论 0原文

我有一个 UITextField,我想通过以下方式启用自动完成:

[self.textView setAutocorrectionType:UITextAutocorrectionTypeYes];

这正常工作,除非我给 UITextView 一个委托。设置委托后,自动完成功能将停止工作。委托只有以下方法:

- (void)textViewDidChange:(UITextView *)textView
{
 self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];

 int left = LENGTH_MAX -[self.textView.text length];
 self.characterCountLabel.text = [NSString stringWithFormat:@"%i",abs(left)];

}

有谁知道如何同时启用自动完成和委托集?

谢谢!
特里斯坦

I have a UITextField that I would like to enable auto completion on by:

[self.textView setAutocorrectionType:UITextAutocorrectionTypeYes];

This works normally, except when I give the UITextView a delegate. When a delegate is set, auto complete just stops working. The delegate has only the following method:

- (void)textViewDidChange:(UITextView *)textView
{
 self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];

 int left = LENGTH_MAX -[self.textView.text length];
 self.characterCountLabel.text = [NSString stringWithFormat:@"%i",abs(left)];

}

Does anyone know how to have both auto complete enabled and a delegate set?

Thanks!
Tristan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-09-02 09:58:25

它可能会破坏自动完成功能,因为您同时修改 UITextView 中的文本。

It's probably breaking autocompletion because you're simultaneously modifying the text in the UITextView.

醉酒的小男人 2024-09-02 09:58:25
NSRange r= [self.textView.text rangeOfString:@"\n"];
if(r.location!=NSNotFound) //must check before replacing new lines right away, otherwise spellcheck gets broken
{
    self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}

该问题是由于每次更改文本时可能会修改文本(即调用替换方法)而引起的。解决方案是仅在必要时调用替换方法。

NSRange r= [self.textView.text rangeOfString:@"\n"];
if(r.location!=NSNotFound) //must check before replacing new lines right away, otherwise spellcheck gets broken
{
    self.textView.text = [self.textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
}

The problem was caused by doing something that potentially modifies the text every time it was changed (ie calling replace method). The solution was to only call the replace method when it was necessary.

梦境 2024-09-02 09:58:25

明白了:您所需要做的就是从界面 (.h) 文件中删除 UITextViewDelegate。

您仍然可以将委托设置为笔尖中的textView。

奇怪吧?为我工作,我希望它也能解决您的问题。

Get this: All you need to do is remove UITextViewDelegate from your interface (.h) file.

You can still set the delegate to the textView in the nib.

Odd, right? Worked for me, I hope it would solve your problem as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文