iOS Swift - 如何向 UITextView 添加不可编辑的后缀
iOS Swift - How to add a uneditable suffix to a UITextView
如何以编程方式向 UITextView 添加后缀 ("kr")?
谢谢!
我假设这是在用户键入以指示货币值时。
在 viewDidLoad 中分配您的 textview 委托(或您希望分配委托的任何地方):
override func viewDidLoad() {
super.viewDidLoad()
myTextView.delegate = self
}
然后在用户输入时将后缀添加到文本视图
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if string.characters.count > 0 {
amountTypedString += string
let newString = amountTypedString + "kr"
myTextView.text = newString
} else {
amountTypedString = String(amountTypedString.characters.dropLast())
if amountTypedString.characters.count > 0 {
let newString = amountTypedString + "kr"
myTextView.text = newString
} else {
myTextView.text = "0kr"
}
}
return false
}
如何以编程方式向 UITextView 添加后缀 ("kr")?
谢谢!
我假设这是在用户键入以指示货币值时。
在 viewDidLoad 中分配您的 textview 委托(或您希望分配委托的任何地方):
override func viewDidLoad() {
super.viewDidLoad()
myTextView.delegate = self
}
然后在用户输入时将后缀添加到文本视图
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if string.characters.count > 0 {
amountTypedString += string
let newString = amountTypedString + "kr"
myTextView.text = newString
} else {
amountTypedString = String(amountTypedString.characters.dropLast())
if amountTypedString.characters.count > 0 {
let newString = amountTypedString + "kr"
myTextView.text = newString
} else {
myTextView.text = "0kr"
}
}
return false
}