0

I have a UITextView that has maxLines set like this:

textView.textContainer.maximumNumberOfLines = 3;
textView.textContainer.lineBreakMode = .byTruncatingTail
textView.isScrollEnabled = false

However, when typing in the textView, you can press return several times and type beyond 3 lines. Visually, only 3 lines appear, but as you type text is being entered on and on. Is there a way to prevent this?

BlueBoy
  • 798
  • 1
  • 11
  • 22

2 Answers2

0

The maximum number of lines is used to define how many will be visible in the interface. This has nothing to do with the amount of text you type.

If you want to add a "limit" to the number of characters you have to do it programmatically.

There are several other answers related to this on SO.

How to limit characters in UITextView iOS

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • I found how to limit the number of characters, but not how to limit the number of lines. Which is why I asked if this was possible with the UITextView itself. – BlueBoy Aug 10 '17 at 01:01
  • No it's not possible with just the UITextView itself, it has to be done programmatically as well. – Pochi Aug 10 '17 at 01:08
0

You can try this

textView.textContainer.maximumNumberOfLines = 3
textView.textContainer.lineBreakMode = .byTruncatingTail
textView.layoutManager.textContainerChangedGeometry(textView.textContainer)
Mohammad Sadiq
  • 5,070
  • 28
  • 29