简体   繁体   English

为什么UITextInput中的InsertText会破坏键盘建议?

[英]Why does InsertText from UITextInput breaks keyboard suggestions?

So we recently had an issue with our Keyboard extension showing wrong suggestions in the accompanying app. 因此,我们最近在键盘扩展程序中遇到了一个问题,即在随附的应用程序中显示了错误的建议。 We found out that the text returned from context ( super.textDocumentProxy.documentContextBeforeInput ) was all wrong and since its a build in apple component something must go wrong with text insertion. 我们发现从上下文( super.textDocumentProxy.documentContextBeforeInput )返回的文本都是错误的,并且由于它是Apple组件中的内置组件,因此文本插入必定会出错。 We had following implementation for inserting text: 我们有以下插入文本的实现:

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

Would return something like: 将返回类似:

There. 那里。 . .

While the real text would be something like: 虽然真实的文本是这样的:

There it was. 在那里。 It was a little test. 这是一个小测试。 Test of everything. 测试一切。

The issue was found to be with insertText apparently it will properly insert text but the Keyboard context will not be able to read the inserted text. 发现问题出在insertText显然可以正确插入文本,但是Keyboard上下文将无法读取插入的文本。 Now the fix was simply to use the replaceCharactersInRange for both insertions (meaning no if / else clause): 现在的解决方法是对两个插入都使用replaceCharactersInRange (意思是if / else子句否):

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

This solved the issue and has made us conclude that insertText doesn't work as intended. 这样就解决了问题,使我们得出结论,insertText不能按预期工作。 Note that this will only be an issue if suggestions are to be used in the text field. 请注意,只有在文本字段中使用建议时,这才是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM