这是我的代码:
func textViewDidChange(_ textView: UITextView) {
if let newCharacters = newTextView.text?.enumerated() {
for (index, item) in newCharacters {
switch item {
case “1”:
newText.text = newTextView.text?.replacingOccurrences(of: “1”, with: “1⃣️”)
case “2”:
newText.text = newTextView.text?.replacingOccurrences(of: “2”, with: “2⃣️”)
case “3”:
newText.text = newTextView.text?.replacingOccurrences(of: “3”, with: “3⃣️”)
default: break
}
}
}
}
这是它的样子:
但是我想实时替换UITextView的所有字符,而不仅仅是文本的最后一个字符。任何想法或建议,将不胜感激。
答案 0 :(得分:2)
发生错误是因为通过用表情符号替换最后一个字符,您将前一个文本返回到textview。
创建一个用于存储表情符号编号的变量,最后,将textview的文本替换为变量的文本。
func textViewDidChange(_ textView: UITextView) {
if let newCharacters = newTextView.text?.enumerated() {
var newText = newTextView.text
for (index, item) in newCharacters {
switch item {
case “1”:
newText?.replacingOccurrences(of: “1”, with: “1⃣️”)
case “2”:
newText?.replacingOccurrences(of: “2”, with: “2⃣️”)
case “3”:
newText?.replacingOccurrences(of: “3”, with: “3⃣️”)
default: break
}
}
newTextView.text = newText
}
}