I want to count newlines ("\n") in a textview.
Or, more specifically, I want to make textview box to count the characters when typing ( +1 ) in label. Also count ( +2 ) when the text have new line and counter ( Label ) must be continuous.
Here is my code:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let allowedChars = 70
let charsInTextView = -txtmessage.text.count
let remainingChars = allowedChars + charsInTextView
if (text == "\n") {
let remainingChars = 70 - (txtmessage.text.count * 2 )
countlabel.text = String(remainingChars)
}
if (text != "\n"){
countlabel.text = String(remainingChars)
}
return true
}