I have the following code in a stand-alone Cocoa test app:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSView *contentView = [window contentView];
NSTextStorage *textStorage = [NSTextStorage new];
NSLayoutManager *layoutManager = [NSLayoutManager new];
NSTextContainer *textContainer = [NSTextContainer new];
[textContainer setHeightTracksTextView:YES];
[textContainer setWidthTracksTextView:YES];
[textStorage addLayoutManager:layoutManager];
[layoutManager addTextContainer:textContainer];
NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:[contentView bounds]];
[scrollView setHasVerticalScroller:YES];
[scrollView setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable];
[scrollView setBorderType:NSNoBorder];
NSRect textFrame;
textFrame.origin = NSZeroPoint;
textFrame.size = [NSScrollView contentSizeForFrameSize:[scrollView frame].size hasHorizontalScroller:NO hasVerticalScroller:YES borderType:NSNoBorder];
NSTextView *textView = [[[NSTextView alloc] initWithFrame:textFrame textContainer:textContainer] autorelease];
[textView setAutoresizingMask:NSViewWidthSizable];
[scrollView setDocumentView:textView];
[contentView addSubview:scrollView];
}
I'm trying to set up the whole hierarchy of objects involved (including the text system objects) in a NSTextView+NSScrollView combination just to see how it all works together. However when I run this and start adding a bunch of lines to the text view, it doesn't scroll when the text gets longer than the view is tall. It's as if the NSScrollView and the NSTextView aren't aware of each other. What connections am I missing to get everything here to communicate correctly?
EDIT: Yes, this is leaky and ugly. :) This was written just to try to determine what's going on here, not production code or anything I'll be using directly ever again. Promise.