NSTextView does not resize with auto-layout

I have a simple layout consisting of NSView and its NSTextView routine. NSTextView is programmatically filled with text that spawns multiple lines. I bind everything together using automatic layout (everything is done programmatically). However, when everything is displayed, NSTextView is disabled, only one row is displayed.

After searching the Internet, the best answer I could find was:

Using Autolayout with the NSTextViews Extension

However, this only works if I manually change the text in the NSTextView after everything is displayed (which is actually not my use case). Views are reconfigured and the entire NSTextView is displayed.

I am trying to figure out when the NSViewController is executing with subviews highlighting so that I can call invalidateIntrinsicContentSize on an NSTextView. Equivalent to viewDidLayoutSubviews in a UIViewController.

So far I have not tried anything. I tried calling invalidateIntrinsicContentSize for NSTextView:

  • At the end of loadView
  • After I filled NSTextView with my text

Is there a better way to achieve this?

+6
source share
1 answer

After further research, I found the answer:

  • Create your own subclass of NSView containing NSTextView
  • In NSView subclass overrides the layout method, which calls invalidateIntrinsicContentSize

Also check out this link, which explains the intricacies of automatic layout and internal content (among many others):

http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html

Code example:

 @interface MyView : NSView @property MyTextView *textView; @end @implementation MyView // init & create content & set constraints -(void) layout { [super layout]; [self.textView invalidateIntrinsicContentSize]; } @end 

MyTextView implementation:

 @implementation MyTextView - (NSSize) intrinsicContentSize { NSTextContainer* textContainer = [self textContainer]; NSLayoutManager* layoutManager = [self layoutManager]; [layoutManager ensureLayoutForTextContainer: textContainer]; return [layoutManager usedRectForTextContainer: textContainer].size; } - (void) didChangeText { [super didChangeText]; [self invalidateIntrinsicContentSize]; } @end 
+6
source

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


All Articles