在我的應用程序中,我對文本限制爲textView
。例如,在description
我有文本限制10000.當textView包含超過10000個字符我只需要在鍵盤中啓用退格鍵,並需要禁用鍵盤中的所有其他鍵,是否有可能。這是我試過的代碼:iOS Swift在鍵盤上只啓用Backspace鍵
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText string: String) -> Bool {
if(textView == DescriptionText)
{
if range.length + range.location > (self.DescriptionText.text?.characters.count)!
{
return false
}
else if range.location == 0 && string == " "
{
return false
}
let NewLength = (self.DescriptionText.text?.characters.count)! - range.length
return NewLength <= 9999
}
else
{
if range.location == 0 && string == " "
{
return false
}
return true
}
}
而不是禁用所有其他鍵,你可以防止輸入正確嗎? –
NewLength計算錯誤。 – YaBoiSandeep
請檢查此https://stackoverflow.com/a/32935626/5523205 – Shahrukh