swift 带有表情符号的光标位置
swift cursor position with emojis
我用来获取 UITextfield 中光标位置的标准方法似乎不适用于某些表情符号。以下代码在插入两个字符后查询文本字段以获取光标位置,首先是表情符号,然后是字母字符。当表情符号插入文本字段时,函数 return 的光标位置值为 2,而不是预期结果 1。关于我做错了什么或如何更正此问题的任何想法。谢谢
这是来自 xcode 游乐场的代码:
class MyViewController : UIViewController {
override func loadView() {
//setup view
let view = UIView()
view.backgroundColor = .white
let textField = UITextField()
textField.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
textField.textColor = .black
view.addSubview(textField)
self.view = view
//check cursor position
var str = ""
textField.insertText(str)
print("cursor position after '\(str)' insertion is \(getCursorPosition(textField))")
textField.text = ""
str = "A"
textField.insertText(str)
print("cursor position after '\(str)' insertion is \(getCursorPosition(textField))")
}
func getCursorPosition(_ textField: UITextField) -> Int {
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.end)
return cursorPosition
}
return -1
}
}
代码return输出如下:
cursor position after '' insertion is 2
cursor position after 'A' insertion is 1
我正在尝试使用光标位置将文本字符串分成两部分——出现在光标之前的文本和出现在光标之后的文本。为此,我使用光标位置作为我使用 map 函数创建的字符数组的索引,如下所示。光标位置导致带有表情符号的数组索引不正确
var textBeforeCursor = String()
var textAfterCursor = String()
let array = textField.text!.map { String([=13=]) }
let cursorPosition = getCursorPosition(textField)
for index in 0..<cursorPosition {
textBeforeCursor += array[index]
}
您的问题是 UITextField selectedTextRange
和 offset
返回的 NSRange
值需要正确转换为 Swift String.Index
。
func getCursorPosition(_ textField: UITextField) -> String.Index? {
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.end)
let positionRange = NSRange(location: 0, length: cursorPosition)
let stringOffset = Range(positionRange, in: textField.text!)!
return stringOffset.upperBound
}
return nil
}
一旦你有了 String.Index
你就可以拆分字符串了。
if let index = getCursorPosition(textField) {
let textBeforeCursor = textField.text![..<index]
let textAfterCursor = textField.text![index...]
}
我用来获取 UITextfield 中光标位置的标准方法似乎不适用于某些表情符号。以下代码在插入两个字符后查询文本字段以获取光标位置,首先是表情符号,然后是字母字符。当表情符号插入文本字段时,函数 return 的光标位置值为 2,而不是预期结果 1。关于我做错了什么或如何更正此问题的任何想法。谢谢
这是来自 xcode 游乐场的代码:
class MyViewController : UIViewController {
override func loadView() {
//setup view
let view = UIView()
view.backgroundColor = .white
let textField = UITextField()
textField.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
textField.textColor = .black
view.addSubview(textField)
self.view = view
//check cursor position
var str = ""
textField.insertText(str)
print("cursor position after '\(str)' insertion is \(getCursorPosition(textField))")
textField.text = ""
str = "A"
textField.insertText(str)
print("cursor position after '\(str)' insertion is \(getCursorPosition(textField))")
}
func getCursorPosition(_ textField: UITextField) -> Int {
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.end)
return cursorPosition
}
return -1
}
}
代码return输出如下:
cursor position after '' insertion is 2
cursor position after 'A' insertion is 1
我正在尝试使用光标位置将文本字符串分成两部分——出现在光标之前的文本和出现在光标之后的文本。为此,我使用光标位置作为我使用 map 函数创建的字符数组的索引,如下所示。光标位置导致带有表情符号的数组索引不正确
var textBeforeCursor = String()
var textAfterCursor = String()
let array = textField.text!.map { String([=13=]) }
let cursorPosition = getCursorPosition(textField)
for index in 0..<cursorPosition {
textBeforeCursor += array[index]
}
您的问题是 UITextField selectedTextRange
和 offset
返回的 NSRange
值需要正确转换为 Swift String.Index
。
func getCursorPosition(_ textField: UITextField) -> String.Index? {
if let selectedRange = textField.selectedTextRange {
let cursorPosition = textField.offset(from: textField.beginningOfDocument, to: selectedRange.end)
let positionRange = NSRange(location: 0, length: cursorPosition)
let stringOffset = Range(positionRange, in: textField.text!)!
return stringOffset.upperBound
}
return nil
}
一旦你有了 String.Index
你就可以拆分字符串了。
if let index = getCursorPosition(textField) {
let textBeforeCursor = textField.text![..<index]
let textAfterCursor = textField.text![index...]
}