[英]How to replace multiple characters of UITextView text in real time with Swift?
这是我的代码:
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的所有字符,而不仅仅是文本的最后一个字符。 任何想法或建议,将不胜感激。
发生错误的原因是,通过用表情符号替换最后一个字符,您会将前一个文本返回到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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.