iosswift4ios-keyboard-extension

KeyboardExtension adjustTextPosition issues with emojis


I'm helping build a keyboardextension and I've recently run into an issue with Swift 4 and emojis. The new UTF-16 emoji support for Swift 4 is really nice but there is an issue with adjustTextPosition in UIInputViewController.

If we call adjustTextPosition to step over an emoji it will simply not step far enough, it seems like the characteroffset used by UIInputViewController doesn't match the character count used by the system.

To test simply write a text with emojis and whenever some key is clicked call:

super.textDocumentProxy.adjustTextPosition(byCharacterOffset: 1)

What can be observed is that we have to click it more than what is to be expected.


Solution

  • Swift 5, it seems that the following code works well on iOS 12.

    let count: Int = String(text).utf16.count
    textDocumentProxy.adjustTextPosition(byCharacterOffset: count)