【问题标题】:How do I add an NSTextView to an NSScrollView?如何将 NSTextView 添加到 NSScrollView?
【发布时间】:2014-02-01 03:58:03
【问题描述】:

This document。解释了如何以编程方式将 NSTextview(可以滚动)添加到窗口。但是,如何以编程方式将 NSTextView 添加到 NSScrollView 中?

编辑:这是适用于 Windows 的代码。我希望能够以编程方式将 NSTextView 插入到我使用 Interface Builder 创建的 NSScrollView 中

-(IBAction)drawTextViews:(id)sender {

NSScrollView *scrollview = [[NSScrollView alloc]

                            initWithFrame:[[theWindow contentView] frame]];

NSSize contentSize = [scrollview contentSize];


NSTextView *theTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0,

                                                           contentSize.width, contentSize.height)];

[theTextView setMinSize:NSMakeSize(0.0, contentSize.height)];

[theTextView setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];

[theTextView setVerticallyResizable:YES];

[theTextView setHorizontallyResizable:NO];

[theTextView setAutoresizingMask:NSViewWidthSizable];



[[theTextView textContainer]

 setContainerSize:NSMakeSize(contentSize.width, FLT_MAX)];

[[theTextView textContainer] setWidthTracksTextView:YES];

[scrollview setDocumentView:theTextView];

[theWindow setContentView:scrollview];

[theWindow makeKeyAndOrderFront:nil];

[theWindow makeFirstResponder:theTextView];*/


[[theTextView enclosingScrollView] setHasHorizontalScroller:YES];

[theTextView setHorizontallyResizable:YES];

[theTextView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

[[theTextView textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];

[[theTextView textContainer] setWidthTracksTextView:NO];

【问题讨论】:

  • 您是否以编程方式创建了 NSScrollView?/
  • 您上面粘贴的链接也对您没有帮助吗?并显示您的代码。到目前为止你尝试了什么??
  • @HussainShabbir 我更新了问题以包含代码。 NSTextField 的滚动视图是以编程方式创建的,但我想将整个 NSTextView 插入到我使用界面生成器创建的另一个 NSScrollView 中
  • 因为你已经创建了另一个滚动视图的出口。所以只需在你的另一个滚动视图中添加文本视图,就像 [Anotherscrollview setDocumentView:theTextView];
  • @HussainShabbir 是的,但这会将整个滚动视图设置为 NSTextView。我只是想让 NSTextView 占用我使用 Interface Builder 创建的 NSScrollView 的一部分

标签: objective-c cocoa


【解决方案1】:

正如 cmets 中所指出的,您可以通过将滚动视图设置为文档视图来在滚动视图中实现 NSTextView。如果您希望 textview 只占用滚动视图的一小部分,则必须以另一种方式构建它。你想达到的目标是什么?

【讨论】:

  • 我想知道如何将文本视图设置为滚动视图的一部分。那我该怎么做呢?
【解决方案2】:

我还遇到了以编程方式创建可滚动 NSTextView 的问题。根据我的尝试,我创建了工作示例应用程序:How to create scrollable NSTextView programmatically?

相关代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat margin = 20;

    NSRect scrollViewRect = NSMakeRect(
        margin,
        margin,
        self.view.bounds.size.width - margin * 2,
        self.view.bounds.size.height - margin * 2
    );

    NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:scrollViewRect];
    scrollview.backgroundColor = [[NSColor blueColor] colorWithAlphaComponent:0.2];
    scrollview.drawsBackground = YES;

    NSSize contentSize = [scrollview contentSize];

    scrollview.borderType = NSGrooveBorder;

    scrollview.hasVerticalScroller = YES;
    scrollview.hasHorizontalScroller = NO;

    scrollview.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;

    NSTextView *theTextView = [[NSTextView alloc] initWithFrame:scrollview.bounds];
    theTextView.drawsBackground = NO;

    theTextView.minSize = NSMakeSize(0.0, contentSize.height);
    theTextView.maxSize = NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX);

    theTextView.verticallyResizable = YES;
    theTextView.horizontallyResizable = NO;

    theTextView.autoresizingMask = NSViewWidthSizable;

    theTextView.textContainer.containerSize = NSMakeSize(contentSize.width, CGFLOAT_MAX);
    theTextView.textContainer.widthTracksTextView = YES;

    NSString *string = @"Once upon a time there was a little-known patent clerk in Bern who received a disappointing annual performance review in ’05";

    [theTextView insertText:string replacementRange:NSMakeRange(0, 0)];

    scrollview.documentView = theTextView;

    [self.view addSubview:scrollview];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多