Row limit in UITextView

I am well aware that this question has been asked, but I cannot find the correct answer. Using a combination of the previous solutions, I came up with this code:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string { int numLines = notesTextView.contentSize.height / notesTextView.font.lineHeight; if (numLines <= 8) { return true; } return false; } 

This does not work, because the number of lines is counted before the additional text, so we still occupy a line other than what we want, and then delay it, since further editing is not possible.

I also tried solutions that detect "\ n" entries, but this does not work, since we can naturally reach new lines without pressing return.

+4
source share
3 answers

I also ran into this problem. None of the early solutions helped me. Here is my solution, I hope: (only iOS 7+!)

 - (void)textViewDidChange:(UITextView *)textView { NSLayoutManager *layoutManager = [textView layoutManager]; NSUInteger numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs]; NSRange lineRange; for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++) { (void) [layoutManager lineFragmentRectForGlyphAtIndex:index effectiveRange:&lineRange]; index = NSMaxRange(lineRange); } if (numberOfLines > 3) { // roll back _labelField.text = _text; } else { // change accepted _text = _labelField.text; } } 

It uses NSString ivar _text to be able to roll back after changing text. It does not flicker.

numberOfLines: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/TextLayout/Tasks/CountLines.html#//apple_ref/doc/uid/20001810-CJBGBIBB

+6
source

How about this:

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string { NSString *temp = [textView.text stringByReplacingCharactersInRange:range withString:string] CGSize size = [temp sizeWithFont:textView.font constrainedToSize:CGSizeMake(textView.frame.size.width,999) lineBreakMode:UILineBreakModeWordWrap]; int numLines = size.height / textView.font.lineHeight; if (numLines <= 8) { return true; } return false; } 

Parse in new text, then check the new text size using the textView information.

+5
source

this is how i did it in the past, hope it helps you.

 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { // limit the number of lines in textview NSString* newText = [mytextView.text stringByReplacingCharactersInRange:range withString:text]; // pretend there more vertical space to get that extra line to check on CGSize tallerSize = CGSizeMake(mytextView.frame.size.width-15, mytextView.frame.size.height*2); CGSize newSize = [newText sizeWithFont:mytextView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap]; if (newSize.height > mytextView.frame.size.height) { NSLog(@"two lines are full"); return NO; } // dismiss keyboard and send comment if([text isEqualToString:@"\n"]) { [mytextView resignFirstResponder]; return NO; } return YES; } 

good luck.

Edit:

Ok try the following method and see if this works for you. Just change the number of rows to whatever number you want.

  (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"n"]) { rows++; if(rows >= maxNumberOfLines){ //Exit textview return NO; } } return YES; 

Let me know if that works out.

0
source

Source: https://habr.com/ru/post/1486847/


All Articles