ios - 带有表情符号设置 : cursorPosition is not correct 的 UITextView.text

标签 ios swift uitextview emoji

此处,str 包含表情符号且设置的 cursorPosition 不正确:

  let cursorPosition = str.characters.count
  let cursorRange = NSRange(location: cursorPosition, length: 0)
  textInputView.selectedRange = cursorRange
  textInputView.scrollRangeToVisible(cursorRange)

最佳答案

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

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

关于ios - 带有表情符号设置 : cursorPosition is not correct 的 UITextView.text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43247038/

相关文章:

ios - 需要在主线程执行什么

ios - 如果核心数据中没有其他与该对象的关系,如何删除对象?

swift - 如何使用 swift 将 Storyboard与 spriteKit 一起使用

swift - 在代码中获取 Realm 版本

iphone - 为什么我不能访问此UITextView实例的.layer.borderWidth? (附加代码)

objective-c - UIImage 导致内存泄漏

ios - IOS 的 Polymer Web 应用程序主屏幕

swift - 无法将类型 'MyEnum<T.Type>' 的值转换为预期的参数类型 'MyEnum<_>'

ios - UITextView 中不需要的滚动空间

ios - 具有动态高度的 UITextView