Execute current UITextField autocorrect suggestion when another button is pressed

冷暖自知 提交于 2019-12-03 11:41:01

问题


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.


回答1:


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.




回答2:


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]];
}



回答3:


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 ;)




回答4:


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



回答5:


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



回答6:


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];
    }
}


来源:https://stackoverflow.com/questions/4611525/execute-current-uitextfield-autocorrect-suggestion-when-another-button-is-presse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!