Reputation: 3401
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 haven'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.
Upvotes: 32
Views: 6510
Reputation: 1822
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
Upvotes: 4
Reputation: 9364
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 ;)
Upvotes: 9
Reputation: 3716
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];
}
}
Upvotes: -1
Reputation: 509
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
Upvotes: 0
Reputation: 2141
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]];
}
Upvotes: 11
Reputation: 185663
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.
Upvotes: 42