代码之家  ›  专栏  ›  技术社区  ›  WoodenKitty

iPhone设置uitextview委派中断自动完成

  •  1
  • WoodenKitty  · 技术社区  · 16 年前

    我有一个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)];
    
    }
    

    有人知道如何启用自动完成和委托集吗?

    谢谢!
    特里斯坦

    3 回复  |  直到 13 年前
        1
  •  0
  •   Shaggy Frog    16 年前

    它可能会破坏自动完成功能,因为您正在同时修改 UITextView .

        2
  •  0
  •   WoodenKitty    16 年前
    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:@""];
    }
    

    该问题是由于每次更改文本时都执行一些可能修改文本的操作(即调用replace方法)。解决方案是只在必要时调用replace方法。

        3
  •  0
  •   Almog C    13 年前

    获取这个:您所需要做的就是从接口(.h)文件中删除uitextviewdelegate。

    您仍然可以在NIB中将委托设置为TEXTVIEW。

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

    推荐文章