title: SwiftUI UIViewRepresentable UITextField set cursor position when typing emoji
body: <p>I'm working with a UITextField wrapped in UIViewRepresentable so I can use it in SwiftUI. I cobbled this together from several SO answers.</p>
  
  <pre><code>struct RADTextField: UIViewRepresentable {
  
      @ObservedObject var dataVM: DataVM
      var placeholder: String
      var isFirstResponder: Bool = false
  
      func makeUIView(context: UIViewRepresentableContext&lt;RADTextField&gt;) -&gt; UITextField {
          let textField = UITextField(frame: .zero)
          textField.font = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body)
          textField.delegate = context.coordinator
          textField.clearButtonMode = .whileEditing
          textField.returnKeyType = .done
          textField.placeholder = self.placeholder
          textField.adjustsFontForContentSizeCategory = true
  
          return textField
      }
  
      func makeCoordinator() -&gt; RADTextField.Coordinator {
          return Coordinator(dataVM: self.dataVM)
      }
  
      func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext&lt;RADTextField&gt;) {
  
          uiView.text = dataVM.fieldOne
          if isFirstResponder &amp;&amp; !context.coordinator.didBecomeFirstResponder  {
              uiView.becomeFirstResponder()
              context.coordinator.didBecomeFirstResponder = true
          }
      }
  
  }
  </code></pre>
  
  <p>Coordinator:</p>
  
  <pre class="lang-swift prettyprint-override"><code>extension RADTextField {
      class Coordinator: NSObject, UITextFieldDelegate {
  
          @ObservedObject var dataVM : DataVM
          var didBecomeFirstResponder = false
  
          init(dataVM: DataVM) {
              self.dataVM = dataVM
          }
  
          func textFieldDidChangeSelection(_ textField: UITextField) {
              self.dataVM.fieldOne = textField.text ?? ""
          }
  
          func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
              textField.resignFirstResponder()
          }
  
          func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {
              // Update Cursor
              let positionOriginal = textField.beginningOfDocument
  
              let cursorLocation = textField.position(from: positionOriginal, offset: (range.location + string.count))
  
              if let txt = textField.text {
                  textField.text = NSString(string: txt).replacingCharacters(in: range, with: string)
              }
              // ✳️ I think this is where the issue is ✳️
              if let cursorLocation = cursorLocation {
                  textField.selectedTextRange = textField.textRange(from: cursorLocation, to: cursorLocation)
              }
  
              return false
          }
      }
  }
  </code></pre>
  
  <p>The problem is that when I try to manually place the cursor somewhere in the text and type an emoji, the next character I type will "split" the emoji. I think some of the emoji characters are actually two characters, but I can't figure out how to deal with that. I marked the code with an ✳️ indicating where I think the issue is.</p>