I'm trying to create an NSTextView that grows vertically as the user types and scrolls once the height has reached a maximum. This is similar to the text view in Messages works.
My first attempt uses the delegate to listen for text changes and adjust the height constraint associated with the NSTextView's scroll view:
- (void)textDidChange:(NSNotification *)notification
{
NSTextView *textView = self.textView;
NSRect usedRect = [textView.textContainer.layoutManager usedRectForTextContainer:textView.textContainer];
NSLog(@"DEBUG: used rect: %@", NSStringFromRect(usedRect));
self.textViewHeightConstraint.constant = MIN(80.f, MAX(usedRect.size.height, 30.f));
}
This almost works: the text view's (scroll view's) height is updated as I type, however, the last line of text is clipped:
Once the scroll view reaches it's max height and begins scrolling it works nicely. I've tried forcing a display/layout/constraint update on the enclosing scroll view with no luck. My guess is the clip view of the scroll view isn't updating correctly, and it's clipping the bottom of the text view. Is there any way to force the clip view/scroll view to update appropriately when the constraint changes?