views:

50

answers:

1

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.

+1  A: 

Have you tried playing with -setHorizontallyResizable: and -setVerticallyResizable:?

Joshua Nozzi
Indeed I did not. And it appears that was the answer. :) Thanks!
Sean
That and -setHeightTracksTextView: needed to be NO.
Sean
I *knew* I was forgetting one, but didn't have time for an exhaustive search. :-)
Joshua Nozzi