如何以编程方式在UITextView中关闭自动更正popover? [英] How can I programmatically dismiss the autocorrect popover in a UITextView?

查看:241
本文介绍了如何以编程方式在UITextView中关闭自动更正popover?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用自己的 insertText:做一些自定义自动完成的东西,但如果有一个自动更正建议可见的视图进入奇怪的状态。 p>

如果我使用 [textView unmarkText] ,它会关闭自动更正弹出窗口 - 但它接受自动更正坏)。有没有办法以编程方式拒绝自动更正建议?



我目前的解决方案是有效的,但它是粗暴和黑客,承担它将在未来继续工作。有更好的方法吗?

   - (void)dismissAutocorrectSuggestionForTextView:(UITextView *)textView {
NSRange range = textView.selectedRange;
textView.text = textView.text;
textView.selectedRange = range;
}


解决方案

,但设置textView的文本这种方式导致textView滚动不必要的(我的textView包含相当多的文本)。我的解决方案涉及以非动画方式恢复contentOffset。这不是什么比你所拥有的更优雅,但至少它帮助那些需要处理更长的文本。



至于它是否会继续工作未来,我已经尝试类似这样的iOS 4以上,并继续通过iOS 6。

   - (void) rejectAutoCorrectSuggestionInTextView:(UITextView *)textView 
{
if([textView isFirstResponder])
{
NSString * original = textView.text;
NSRange originalRange = textView.selectedRange;
CGPoint originalOffset = textView.contentOffset;

//强制应用任何挂起的自动更正
[textView resignFirstResponder];
[textView becomeFirstResponder];

NSString * final = textView.text;

if(![original isEqualToString:final])
{
textView.text = original;
textView.selectedRange = originalRange;
[textView setContentOffset:originalOffset animated:NO];
}
}
}


I'm doing some custom auto-complete stuff on my own with insertText:, but if there's an autocorrect suggestion visible the view gets into a weird state.

If I use [textView unmarkText], it dismisses the autocorrect popup thingy -- but it accepts the autocorrection (which is bad). Is there some way to programmatically reject the autocorrect suggestion?

My current "solution" works, but it's gross and hacky and I have no reason to assume it will continue to work in the future. Is there a better way to do this?

- (void)dismissAutocorrectSuggestionForTextView:(UITextView *)textView {
    NSRange range = textView.selectedRange;
    textView.text = textView.text;
    textView.selectedRange = range;
}

解决方案

I tried something similar to yours, but setting the text of the textView this way results in the textView scrolling unnecessarily (my textView contains quite a bit of text). My solution involves restoring the contentOffset in an non-animated fashion. It's not exactly any more elegant than what you have, but at least it helps those who need to deal with longer text.

As for whether it'll continue to work in future, I've tried something like this since iOS 4, and it continues to work through iOS 6.

- (void)rejectAutoCorrectSuggestionInTextView:(UITextView *)textView
{
    if ([textView isFirstResponder])
    {
        NSString *original = textView.text;
        NSRange originalRange = textView.selectedRange;
        CGPoint originalOffset = textView.contentOffset;

        // Force any pending autocorrection to be applied
        [textView resignFirstResponder];
        [textView becomeFirstResponder];

        NSString *final = textView.text;

        if (![original isEqualToString:final])
        {
            textView.text = original;
            textView.selectedRange = originalRange;
            [textView setContentOffset:originalOffset animated:NO];
        }
    }
}

这篇关于如何以编程方式在UITextView中关闭自动更正popover?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆