Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextView inside an NSScrollView doesn't scroll :(

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.

like image 526
Sean Avatar asked Aug 25 '10 19:08

Sean


1 Answers

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

like image 85
Joshua Nozzi Avatar answered Nov 15 '22 10:11

Joshua Nozzi