0

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

2 Answers 2

2

You should store the bool values instead of returning it and combine them with an && and return it.

let existingLines = textView.text.components(separatedBy: CharacterSet.newlines) let newLines = text.components(separatedBy: CharacterSet.newlines) let linesAfterChange = existingLines.count + newLines.count - 1 let linesCheck = linesAfterChange <= textView.textContainer.maximumNumberOfLines let newText = (textView.text as NSString).replacingCharacters(in: range, with: text) let characterCountCheck = newText.utf16.count < 140 return linesCheck && characterCountCheck 

Sidenote: Avoid using NSString in Swift. You can do the same thing with String.

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if let textViewString = textView.text, let range = Range(range, in: textViewString) { let newString = textViewString.replacingCharacters(in: range, with: text) } return condition } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your reply. I have already tried this, but it does not work as expected. For instance: If I write 139 characters, then use a new line - it is not possible to do any input at all. Not even backspace to remove previously entered text. The cursor is stuck in the position it is in.
@fisher when you are 139, you would have probably reached the 3 line limit?
Nope, I have not. And it is not possible to use the back key either. So something is not working properly. I’ll do some more testing!
@fisher can you share the rest of the class?
My bad, I was adjusting the maxLimit (140) dynamically - reducing the maxLimit by 10 for each newline entered. This is what lead to the freeze. I removed the dynamic adjustment and it now works as expected. Thanks for your patience and help!
0

Combine the boolean values with && (and operator) and return the result

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { let existingLines = textView.text.components(separatedBy: CharacterSet.newlines) let newLines = text.components(separatedBy: CharacterSet.newlines) let linesAfterChange = existingLines.count + newLines.count - 1 let newText = (textView.text as NSString).replacingCharacters(in: range, with: text) return linesAfterChange <= textView.textContainer.maximumNumberOfLines && newText.utf16.count < 140 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.