UITextView.text 带有表情符号设置:cursorPosition 不正确

Li.*_*Rui 2 uitextview ios emoji swift

这里,str包含表情符号并且设置的光标位置不正确:

  let cursorPosition = str.characters.count
  let cursorRange = NSRange(location: cursorPosition, length: 0)
  textInputView.selectedRange = cursorRange
  textInputView.scrollRangeToVisible(cursorRange)
Run Code Online (Sandbox Code Playgroud)

Nir*_*v D 5

看起来您想将光标放在 的最后textView。像这样尝试一下。

textInputView.becomeFirstResponder()
let cursorPosition = str.utf16.count
let cursorRange = NSRange(location: cursorPosition, length: 0)
textInputView.selectedRange = cursorRange
textInputView.scrollRangeToVisible(cursorRange)
Run Code Online (Sandbox Code Playgroud)