Limit the number of rows for a UITextview

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

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

Where do I need to start? Am I doing this using a method? AT

- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView 
+56
iphone cocoa-touch uitextview
Mar 07 2018-11-11T00:
source share
8 answers

You have the right idea, but the wrong method. textView:shouldChangeTextInRange:replacementText: is called whenever the text changes; you can access the current content of the text view using its text property, and you can create new content from the range that has been passed and replace the text [textView.text stringByReplacingCharactersInRange:range withString:replacementText] . You can then count the number of rows and return YES to allow the change, or NO to reject it.

+15
Mar 08 2018-11-11T00:
source share

Maciek Czarnik's answer did not work for me, but it let me know what to do.

iOS 7+

Swift

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

Objc

 textView.textContainer.maximumNumberOfLines = 10; textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail; 
+175
Jan 06 '15 at 7:26
source share

Maybe this might help (iOS 7+):

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

Even the first line should do the trick, I think, but not ... Maybe his error in the SDK

+50
Jan 15 '14 at 22:08
source share

Maciek Czarnik's answer does not seem to work for me, even in iOS7. This gives me strange behavior, I don’t know why.

What I'm doing to limit the number of rows in a UITextView is simply:

(tested only in iOS7) 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]; } } 
+11
Apr 02 '14 at 14:20
source share

in Swift 3.0 version:

 self.textView.textContainer.maximumNumberOfLines = self.textViewNumberOflines self.textView.textContainer.lineBreakMode = .byTruncatingTail 
+11
Jan 04 '17 at 4:16
source share

Here's an improved version of Numereyes answer in Swift 4

I made a small extension so that I can reuse the code. I use a while loop to check if the size fits. This also works when the user inserts a lot of text at the same time.

 extension UITextView { var numberOfCurrentlyDisplayedLines: Int { let size = systemLayoutSizeFitting(UILayoutFittingCompressedSize) //for Swift 4.2, replace with next line: //let size = systemLayoutSizeFitting(UIView.layoutFittingCompressedSize) return Int(((size.height - layoutMargins.top - layoutMargins.bottom) / font!.lineHeight)) } /// Removes last characters until the given max. number of lines is reached func removeTextUntilSatisfying(maxNumberOfLines: Int) { while numberOfCurrentlyDisplayedLines > (maxNumberOfLines) { text = String(text.dropLast()) layoutIfNeeded() } } } // Use it in UITextView delegate method: func textViewDidChange(_ textView: UITextView) { textView.removeTextUntilSatisfying(maxNumberOfLines: 10) } 
+2
Dec 06 '17 at 14:16
source share

Other solutions given do not solve the problem associated with the last line created at the end (11th line in the question).

Here is a working solution with Swift 4.0 and Xcode 9.0 beta (found on this blog post )

  class ViewController: UIViewController, UITextViewDelegate { @IBOutlet weak var textView: UITextView! override func viewDidLoad() { super.viewDidLoad() textView.delegate = self textView.textContainer.maximumNumberOfLines = 10 textView.textContainer.lineBreakMode = .byWordWrapping } func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { let existingLines = textView.text.components(separatedBy: CharacterSet.newlines) let newLines = text.components(separatedBy: CharacterSet.newlines) let linesAfterChange = existingLines.count + newLines.count - 1 return linesAfterChange <= textView.textContainer.maximumNumberOfLines } 

Nota bene: this solution does not handle a scenario in which the last line is too long to display (the text will be hidden on the right side of the UITextView).

+1
Aug 28 '17 at 13:01
source share

Like other answers, but can be used directly from the Storyboard and without a subclass:

 extension UITextView { @IBInspectable var maxNumberOfLines: NSInteger { set { textContainer.maximumNumberOfLines = maxNumberOfLines } get { return textContainer.maximumNumberOfLines } } @IBInspectable var lineBreakByTruncatingTail: Bool { set { if lineBreakByTruncatingTail { textContainer.lineBreakMode = .byTruncatingTail } } get { return textContainer.lineBreakMode == .byTruncatingTail } } } 
+1
Nov 20 '17 at 1:37
source share



All Articles