I need a textView to have a line limit where return (or new line) is disabled for the fifth line, in other words, max 4 lines inside textView. I've tried some things and this is the closest I've come but I can't get it to work:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" { // Line added
lineCount += 1
if lineCount == 5 {
print("No more! lineCount at \(lineCount)")
lineCount -= 1
textView.deleteBackward()
}
} else if !text.isEmpty { // Character added
descCount += 1
} else if range.length == 1 && textView.text.characters.last == "\n" { // Line deleted
lineCount -= 1
} else if text == "" && descCount != 0 { // Character deleted
descCount -= 1
}
let newLength = descCount + text.characters.count - range.length - lineCount // text.characters.count counts /n characters
return newLength <= 202 // Arbitrary number
}
Thanks in advance.