In my text view
I would like autocorrect to be disabled when typing words that start with "@". The reason being is because I have a tableview menu that popups and suggests usernames. If a username is selected the current text is replaced with a hyperlink
. This functionality is very similar to Facebook
.
Everything works great if autocorrect is disabled on my text view. However if I enable autocorrect on my text view it messes up at times because of the suggested text. Here is my code where I am attempting to change the text view's autocorrect property in the delegate protocol:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:@"@"]) {
// text starts with "@" so we disable autocorrect
textView.autocorrectionType = UITextAutocorrectionTypeNo;
} else if ([text isEqualToString:@" "]) {
// empty space so we reenable autocorrect
textView.autocorrectionType = UITextAutocorrectionTypeYes;
}
return YES;
}
For whatever reason it's not working even though the autocorrect property is correctly being changed. I verified the textview autocorrect property in the debugger and it's definitely being changed but the behavior while I am typing isn't. The auto correct never disables when needed because the autocorrect menu still appears below the current text:
Edit: Got it working. The best solution seems to be to changing your responder to another textview that is hidden, set the original text views autocorrectType property and then reassign the original text view as the first responder. It's a little hacky but it works and doesn't cause the keyboard to jump. Also it's important to check the textViews current autocorrectionType property to prevent redundant responder assignments. Many thanks to Lyndsey for helping me get to the answer.
(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if ([text isEqualToString:@"@"] && textView.autocorrectionType != UITextAutocorrectionTypeNo) {
// text starts with "@" so we disable autocorrect [self.hiddenTextView becomeFirstResponder]; textView.autocorrectionType = UITextAutocorrectionTypeNo; [textView becomeFirstResponder];
} else if ([text isEqualToString:@" "] && textView.autocorrectionType != UITextAutocorrectionTypeYes) {
// empty space so we reenable autocorrect [self.hiddenTextView becomeFirstResponder]; textView.autocorrectionType = UITextAutocorrectionTypeYes; [textView becomeFirstResponder];
}
return YES; }