Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView - Setting max lines hides the cursor

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.

like image 744
Nitish Avatar asked Mar 24 '17 07:03

Nitish


2 Answers

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];
}
like image 95
byJeevan Avatar answered Oct 08 '22 00:10

byJeevan


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)
like image 27
Owen Zhao Avatar answered Oct 08 '22 01:10

Owen Zhao