按下另一个按钮时执行当前的 UITextField 自动更正建议

发布于 2024-10-10 17:52:13 字数 238 浏览 2 评论 0原文

我正在我的应用程序中实现聊天功能,与 iPhone 的内置消息应用程序非常相似。我在按钮旁边有一个 UITextField。用户在文本字段中输入内容,文本字段通常会建议各种自动更正。在内置的消息应用程序中,点击发送按钮将导致执行当前可见的自动更正建议。我正在我的应用程序中寻找这种行为,但没有找到任何东西。

有谁知道当激活完全独立的控件时以编程方式执行 UITextField 当前可见的自动更正/自动完成建议的方法?显然这在某种程度上是可能的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

定格我的天空 2024-10-17 17:52:13

在字段上调用 ​​-resignFirstResponder。这迫使它接受自动更正。如果您不想关闭键盘,可以立即再次调用 -becomeFirstResponder

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.

蓝天 2024-10-17 17:52:13

对于 esilver:您可以通过让不同的文本字段成为FirstResponder,然后让相关的文本字段成为FirstResponder 来完成此操作,而无需放弃第一响应者。在这种情况下键盘不会移动,也不会触发任何隐藏通知。如果您没有任何其他文本字段,请创建一个虚拟文本字段并将其设置为隐藏 = 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]];
}

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]];
}
︶ ̄淡然 2024-10-17 17:52:13

由于辞职并重新担任第一响应者可能会产生副作用(大量通知、键盘显示/隐藏触发器等),因此我一直在寻找一种替代的、不那么残酷的方法。经过相当多的搜索后,我发现这就是您在 UITextView (或 UITextField fwiw)中接受自动更正所需要做的全部事情:

[textView.inputDelegate selectionWillChange: textView];
[textView.inputDelegate selectionDidChange: textView];

希望这会有所帮助;)

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

懒的傷心 2024-10-17 17:52:13

这段代码解决了我的问题。

[self.textView rejectAutoCorrect];

类别代码。

@implementation UITextView (rejectAutoCorrect)

- (void)rejectAutoCorrect
{
    if ([self isFirstResponder])
    {
        [self.inputDelegate selectionWillChange:self];
        [self.inputDelegate selectionDidChange:self];
    }
}

@end

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
滴情不沾 2024-10-17 17:52:13

在 Nick Locking 的建议的基础上,我们编写了一个类别方法,用于处理任何待处理的自动更正建议,而无需关闭键盘(并且不会触发 will/did 隐藏/显示通知)。

@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

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
无名指的心愿 2024-10-17 17:52:13

这是所讨论的解决方案的快速清晰回顾...

创建一个虚拟文本视图来制作响应者,然后返回到原始文本视图。确保它是第一响应者。

设置:

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

方法:

- (void)commitSuggestions {
    if([self.textView isFirstResponder]) {
        [self.dummyTextView becomeFirstResponder];
        [self.textView becomeFirstResponder];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文