【问题标题】:Why does InsertText from UITextInput breaks keyboard suggestions?为什么 UITextInput 中的 InsertText 会破坏键盘建议?
【发布时间】:2017-11-07 04:51:04
【问题描述】:

因此,我们最近遇到了一个问题,我们的键盘扩展程序在随附的应用程序中显示了错误的建议。我们发现从上下文返回的文本 (super.textDocumentProxy.documentContextBeforeInput) 都是错误的,并且由于它是内置的苹果组件,因此文本插入肯定有问题。我们有以下插入文本的实现:

    if (replaceRange.length > 0) {
        [self.textView.textStorage replaceCharactersInRange:replaceRange withString:selectedWord];
        [textView setSelectedRange:NSMakeRange(newLocation, 0)];  //Place cursor after inserted word
    } else {
        [self.textView insertText:selectedWord];
    }

会返回类似:

那里。 . .

而真正的文本会是这样的:

就在那里。这是一个小测试。测试一切。

【问题讨论】:

    标签: ios keyboard uitextinput


    【解决方案1】:

    发现问题出在insertText 上,显然它会正确插入文本,但键盘上下文将无法读取插入的文本。现在解决方法是简单地将replaceCharactersInRange 用于两个插入(意味着没有 if / else 子句):

    [self.textView.textStorage replaceCharactersInRange:replaceRange withString:selectedWord];
            [textView setSelectedRange:NSMakeRange(newLocation, 0)];  //Place cursor after inserted word
    

    这解决了问题并让我们得出结论 insertText 无法按预期工作。请注意,只有在文本字段中使用建议时,这才会成为问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-30
      • 2018-10-12
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多