views:

272

answers:

2

Hi there!

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

A: 

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

Shaggy Frog
A: 
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.

Tristan