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:
class ViewController: UIViewController,UITextViewDelegate {
}

We have to set the delegate to the textView in Storyboard or via code.
                TextView.delegate = self

Then implement the below method to control the input from the user.



// #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
    }
The above I had put the maximum character count as 64.

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift