4 Replies
      Latest reply on May 23, 2020 7:45 AM by plkmo
      plkmo Level 1 Level 1 (0 points)

        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|

        Zxzxzxzxzxzxc -> 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, "Zxzxzxzxzxzxc" 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.