私のアプリでは、一部のtextView
のテキストの制限があります。たとえば、In description
ではテキストの制限が10000です。textViewに10000文字以上の文字が含まれている場合、キーボードのバックスペースキーを有効にしてキーボードの他のすべてのキーを無効にする必要があります。 textfieldShouldChangeでiOS Swiftキーボードのバックスペースキーのみ有効
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