How to set the maximum characters allowed to type from the User for UITextView in iOS Swift
We can set the maximum allowed characters to get the input from the user in swift.First, we have to add the delegate to the class.
Code:
We have to set the delegate to the textView in Storyboard or via code.
Then implement the below method to control the input from the user.
The above I had put the maximum character count as 64.
class ViewController: UIViewController,UITextViewDelegate {
}
We have to set the delegate to the textView in Storyboard or via code.
-
TextView.delegate = self
// #MARK: - UITextViewDelegate
func textView(_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String) -> Bool{
guard let texts = textView.text else { return true }
let newLength = texts.utf16.count + text.utf16.count - range.length
return newLength <= 64
// Bool
}
Comments
Post a Comment