我在独立的 Cocoa 测试应用程序中有以下代码:
- (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];
}
我试图在 NSTextView+NSScrollView 组合中设置所涉及对象的整个层次结构(包括文本系统对象),只是为了看看它们是如何一起工作的。但是,当我运行它并开始向文本视图添加一堆行时,当文本长度超过视图高度时,它不会滚动。就好像 NSScrollView 和 NSTextView 不知道对方。我缺少什么连接才能让这里的所有东西都能正确通信?
编辑:是的,这是泄漏和丑陋的。:) 写这篇文章只是为了确定这里发生了什么,而不是生产代码或我将再次直接使用的任何东西。承诺。