I have a non-scrollable UITextView with it\'s layoutManager maximumNumberOfLines set to 9, which works fine, but, I cannot seem to find a method in NSLayoutManager that rest
boundingRectWithSize:options:attributes:context:
is not recommended for textviews, because it does not take various attributes of the textview (such as padding), and thus return an incorrect or imprecise value.
To determine the textview's text size, use the layout manager's usedRectForTextContainer:
with the textview's text container to get a precise rectangle required for the text, taking into account all required layout constraints and textview quirks.
CGRect rect = [self.textView.layoutManager usedRectForTextContainer:self.textView.textContainer];
I would recommend doing this in processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:
, after calling the super
implementation. This would mean replacing the textview's layout manager by providing your own text container and setting its layout manager to your subclass' instance. This way you can commit the changes from the textview made by the user, check if the rect is still acceptable and undo if not.