manatee manatee - 1 year ago 82
iOS Question

How do I cut off the text view after 20 characters?

I am trying to limit the number of characters inside the text view to 20. After 20 it should instead have "...". The function is not firing and I am setting the delegate correctly.

Animal class = np[indexPath.row] = false = self

Extension of Animal class

extension Animal : UITextViewDelegate{
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
return textView.text.characters.count + (text.characters.count - range.length) <= 20

Answer Source

You can use something like this:

            if displayName.characters.count > 20 {
                displayName = (displayName as NSString).substringToIndex(20)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download