I am trying to replace user input text with some edited text. I have implemented the below function to do this:
func replaceText(replacementString string: String) {
if let afterInput = textDocumentProxy.documentContextAfterInput {
textDocumentProxy.adjustTextPosition(byCharacterOffset: afterInput.count)
}
if let word:String = textDocumentProxy.documentContextBeforeInput
{
for _: Int in 0 ..< word.count {
textDocumentProxy.deleteBackward()
}
}
textDocumentProxy.insertText(string)
}
Thereafter, I will use this as follows to replace text in any user input string:
delegate?.replaceText(replacementString: "Test")
where delegate points to the keyboard viewcontroller.
Input -> Output ( | denotes the cursor position):
Zxzxzxzxzx| -> Test|
Zxzxzxzxzx😉zxc -> Test|xc
However, it doesn't seem to work as planned, as you can see from the above input -> output.
Eg. For normal text without emojis, "Zxzxzxzxzx" gets replaced by "Test" as expected. But with emoji, "Zxzxzxzxzx😉zxc" gets replaced by "Test|xc", and the cursor is now in the middle of the text.
It seems to break when emojis are present in the text. I have printed out the word counts and it seems consistent. Any ideas? Thanks in advance.