0

I'm trying to limit the maximum lines of my textview to 2 lines. Ive tried:

This allows returns on the 3rd line

 func textViewDidChange(textView: UITextView) { 

  let  numLines:CGFloat = textView.contentSize.height/textView.font.lineHeight;
    if (numLines > 2.999999)
    {
       textView.text = textView.text.substringToIndex(advance(textView.text.endIndex, -1))
    }        
}

This has some weird behavior on returns

textView.textContainer.maximumNumberOfLines = 2;

Any suggestions on how I can limit my textview to two lines?

Léo Natan
  • 56,823
  • 9
  • 150
  • 195

1 Answers1

0

Try this:

textView.maxNumberOfLines = 2
Andy
  • 49,085
  • 60
  • 166
  • 233
sik
  • 1
  • 1
  • 3
  • that code doesnt work unless I put textContainer inbetween them and I've already tried that –  Jul 28 '15 at 18:45
  • http://stackoverflow.com/a/27793974/3983725 this answer worked for me, just add textView.textContainer.lineBreakMode = NSLineBreakMode.ByTruncatingTail, after setting the maximumNumberOfLines – sik Jul 28 '15 at 18:57
  • maximumNumberOfLines has weird behavior. After returning the maximum amount of times and then typing again it writes more text above the first line –  Jul 28 '15 at 19:03
  • While this might answer the question, without any explanation it will stay at best marginal. In point of fact, it was auto-flagged as VLQ for that reason. – Deduplicator Jul 28 '15 at 19:41