Execute current UITextField autocorrect suggestion when another button is pressed
I am implementing chat in my application, very similar to the iPhone's built-in Messages app. I have a UITextField next to a button. The user types something into the text field, and very often the text field suggests various autocorrections. In the built-in Messages app, tapping the Send button will cause the currently visible autocorrection suggestion to execute. I am seeking this behavior in my application, but have开发者_开发技巧n't been able to find anything.
Does anyone know of a way to programmatically execute the currently visible autocorrection/autocomplete suggestion of a UITextField when a completely separate control is activated? It's obviously possible somehow.
Call -resignFirstResponder
on the field. That forces it to accept the autocorrect. If you don't want to dismiss the keyboard, you can immediately follow that with a call to -becomeFirstResponder
again.
For esilver: you can do this without resigning first responder by having a different textfield becomeFirstResponder and then having the relevant textfield becomeFirstResponder. The keyboard will not move in this case, and not trigger any hide notifications. If you don't have any other textfields, create a dummy textfield and set it to hidden = YES.
-(void)tappedSendButton:(id)sender
{
// This hack is in place to force auto-corrections to be applied
// before the text is sent.
[self.dummyTextField becomeFirstResponder];
[self.toolbar.textView becomeFirstResponder];
[self sendChatWithBody: [self.toolbar.textView.text copy]];
}
Since resigning and re-assuming first responder may have side effects (lots of notifications, keyboard show/hide triggers, etc), I've been looking for an alternative, less brutal way. After quite some search, I found this is all you need to do to accept autocorrections in a UITextView
(or UITextField
fwiw):
[textView.inputDelegate selectionWillChange: textView];
[textView.inputDelegate selectionDidChange: textView];
Hope this helps ;)
This code was solve problem in my case.
[self.textView rejectAutoCorrect];
Category code.
@implementation UITextView (rejectAutoCorrect)
- (void)rejectAutoCorrect
{
if ([self isFirstResponder])
{
[self.inputDelegate selectionWillChange:self];
[self.inputDelegate selectionDidChange:self];
}
}
@end
Building on Nick Locking's suggestions, here is a category method we wrote to process any pending autocorrect suggestions without dismissing the keyboard (and without triggering the will/did hide/show notifications).
@implementation UITextView (SuggestionHelpers)
- (void)acceptSuggestionWithoutDismissingKeyboard {
// by making another UITextField the first responder, the keyboard won't try to hide
UITextField *temp = [[[self class] alloc] initWithFrame:CGRectZero];
temp.hidden = YES;
[[self superview] addSubview:temp];
[temp becomeFirstResponder];
[self becomeFirstResponder];
[temp removeFromSuperview];
}
@end
Here's a quick clean recap of solutions discussed...
Create a dummy textview to make responder and then return to original textview. make sure it was first responder.
setup:
self.textView = [[UITextView alloc] initWithFrame:self.view];
[self.view addSubview:self.textView];
self.dummyTextView = [[UITextView alloc] init];
[self.dummyTextView setHidden:YES];
[self.view addSubview:self.dummyTextView];
Method:
- (void)commitSuggestions {
if([self.textView isFirstResponder]) {
[self.dummyTextView becomeFirstResponder];
[self.textView becomeFirstResponder];
}
}
精彩评论