NSScrollView 内的 NSTextView 不滚动:(

发布于 2024-09-15 15:59:02 字数 1563 浏览 4 评论 0原文

我在独立的 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 不知道彼此。为了使这里的所有内容都能正确通信,我缺少哪些连接?

编辑:是的,这是泄漏且丑陋的。 :) 编写本文只是为了尝试确定这里发生的情况,而不是生产代码或我将再次直接使用的任何内容。承诺。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文