Setting the maximum number of rows in a UITextView

I would like to limit the user of my iPad application to a maximum of 20 lines when output to pdf. The user enters text into a UITextView (as shown below) and then converts to a pdf document.

enter image description here

I understand that I will have to do several calculations depending on what text is entered as they are entered. Are there any methods that can help me here? textFieldDidEndEditing allows me to check after it has been entered.

Also, since that would be much simpler, is there a way to limit the number of rows a UITextField ?

+7
ios uitextfield ipad
source share
4 answers

You were closer to the delegate method textFieldDidEndEditing .

It is best to respond to the delegate method textView:shouldChangeTextInRange:replacementText: and then see if there is a change that should happen that will increase the number of rows.

Here is an example of how to do this:

 - (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { static const NSUInteger MAX_NUMBER_OF_LINES_ALLOWED = 3; NSMutableString *t = [NSMutableString stringWithString: self.textView.text]; [t replaceCharactersInRange: range withString: text]; NSUInteger numberOfLines = 0; for (NSUInteger i = 0; i < t.length; i++) { if ([[NSCharacterSet newlineCharacterSet] characterIsMember: [t characterAtIndex: i]]) { numberOfLines++; } } return (numberOfLines < MAX_NUMBER_OF_LINES_ALLOWED); } 

I will give a complete example on the Internet https://github.com/st3fan/StackOverflow/tree/master/TextViewWithLineLimit . tested on iOS 7.0.2. Enjoy it!

+11
source share

The following works with support for '\ n' + wrapped line.

 - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.AutoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; self.AutocorrectionType = UITextAutocorrectionTypeNo; self.KeyboardAppearance = UIKeyboardAppearanceAlert; self.font = [UIFont fontWithName:@"Avenir" size:14]; self.maximumNumberOfLinesAllowed = 4; self.delegate = self; } return self; } - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText { NSMutableString *t = [NSMutableString stringWithString: self.text]; [t replaceCharactersInRange: aRange withString: aText]; // First check for standard '\n' (newline) type characters. NSUInteger numberOfLines = 0; for (NSUInteger i = 0; i < t.length; i++) { if ([[NSCharacterSet newlineCharacterSet] characterIsMember: [t characterAtIndex: i]]) { numberOfLines++; } } if (numberOfLines >= self.maximumNumberOfLinesAllowed) return NO; // Now check for word wrapping onto newline. NSAttributedString *t2 = [[NSAttributedString alloc] initWithString:[NSMutableString stringWithString:t] attributes:@{NSFontAttributeName:self.font}]; __block NSInteger lineCount = 0; CGFloat maxWidth = self.frame.size.width; NSLog(@"maxWidth = %.02f", maxWidth); NSTextContainer *tc = [[NSTextContainer alloc] initWithSize:CGSizeMake(maxWidth, CGFLOAT_MAX)]; NSLayoutManager *lm = [[NSLayoutManager alloc] init]; NSTextStorage *ts = [[NSTextStorage alloc] initWithAttributedString:t2]; [ts addLayoutManager:lm]; [lm addTextContainer:tc]; [lm enumerateLineFragmentsForGlyphRange:NSMakeRange(0,lm.numberOfGlyphs) usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop) { lineCount++; }]; // NSLog(@"%d", lineCount); return (lineCount <= self.maximumNumberOfLinesAllowed); } 
+3
source share

To limit the number of rows in a UITextView, I just do:

In the following UITextViewDelegate method:

 - (void)textViewDidChange:(UITextView *)textView { NSUInteger maxNumberOfLines = 5; NSUInteger numLines = textView.contentSize.height/textView.font.lineHeight; if (numLines > maxNumberOfLines) { textView.text = [textView.text substringToIndex:textView.text.length - 1]; } } 
+2
source share

much easier to do the following:

Swift:

 textView.textContainer.maximumNumberOfLines = 10; textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail; 

Obj-C:

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

All Articles