Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the number of lines for UITextview

I was wondering how to limit the amount of LINES (not characters as asked in other questions) a user can enter when editing a UITextField.

Ideally, I would like to limit the input to max. 10 lines.

Where would I need to start? Do I do this with a method? In

 - (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView
like image 491
n.evermind Avatar asked Mar 07 '11 21:03

n.evermind


2 Answers

Maciek Czarnik answer does not worked for me, but it got me insights what to do.

iOS 7+

Swift

textView.textContainer.maximumNumberOfLines = 10
textView.textContainer.lineBreakMode = .byTruncatingTail

ObjC

textView.textContainer.maximumNumberOfLines = 10;
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
like image 149
Jamagas Avatar answered Nov 11 '22 03:11

Jamagas


Maybe this can help (iOS 7+):

textView.textContainer.maximumNumberOfLines = 10;
[textView.layoutManager textContainerChangedGeometry:textView.textContainer];

Even first line should do the trick I guess, but doesn't... Maybe its a bug in SDK

like image 58
Maciek Czarnik Avatar answered Nov 11 '22 03:11

Maciek Czarnik