I am using shouldChangeTextIn on the UITextView and I am able to limit the TextView to either a maximum of 4 lines OR a maximum of 140 characters using the following code in shouldChangeTextIn:
Max 4 lines:
let existingLines = textView.text.components(separatedBy: CharacterSet.newlines) let newLines = text.components(separatedBy: CharacterSet.newlines) let linesAfterChange = existingLines.count + newLines.count - 1 return linesAfterChange <= textView.textContainer.maximumNumberOfLines Max 140 characters:
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text) return newText.utf16.count < 140 However, I want to combine these two so it checks for both and I am not able to figure it out. Can anyone guide me in the right direction?
Best regards, Erik