8

This is what I am doing to set max number of lines :

self.text_description.textContainer.maximumNumberOfLines = 2
self.text_description.layoutManager.textContainerChangedGeometry(self.text_description.textContainer)  

When 3rd line is about to get started, the cursor disappears. To put it back I have to tap backspace.

Nitish
  • 13,845
  • 28
  • 135
  • 263

2 Answers2

3

Assuming, in the text view cursor doesn't stays behind the keyboard. I have written a helper method that makes UITextField text scroll to end of text.

- (void)scrollToCaretInTextView:(UITextView *)textView animated:(BOOL)animated
{
    CGRect rect = [textView caretRectForPosition:textView.selectedTextRange.end]; //Get the content size of textview 
    rect.size.height += textView.textContainerInset.bottom;
    [textView scrollRectToVisible:rect animated:animated];
}
byJeevan
  • 3,728
  • 3
  • 37
  • 60
2

I don't quite sure what you are expecting. If you want two lines max, there should be no 3rd line.

Use this:

self.text_description.textContainer.maximumNumberOfLines = 2
self.text_description.textContainer.lineBreakMode = .byTruncatingTail

Limit the number of lines for UITextview

It seems what you actually want is a textview with two lines height? If that is true, try this in you view controller.

self.text_description = UITextView(frame: CGRect(x: 100.0, y: 100.0, width: 200.0, height: 28.0))
self.text_description.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
self.view.addSubview(self.text_description)
Community
  • 1
  • 1
Owen Zhao
  • 3,205
  • 1
  • 26
  • 43