Objective c 如何将NSTextView添加到NSScrollView?

Objective c 如何将NSTextView添加到NSScrollView?,objective-c,cocoa,Objective C,Cocoa,。说明如何以编程方式将NSTextview(可滚动)添加到窗口。但是,如何以编程方式将NSTextView添加到NSScrollView中 编辑:以下是适用于windows的代码。我希望能够以编程方式将NSTextView插入到使用Interface Builder创建的NSScrollView中 -(IBAction)drawTextViews:(id)sender { NSScrollView *scrollview = [[NSScrollView alloc]

。说明如何以编程方式将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];

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

我还遇到了可滚动NSTextView的编程创建问题。根据我的尝试,我创建了工作示例应用程序:

以下是相关代码:

- (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];
}

您是否以编程方式创建了NSScrollView?/此外,您在上面粘贴的链接对您没有帮助,并且还显示了您的代码。到目前为止你尝试了什么???@HussainShabbir我更新了问题以包含代码。NSTextField的scrollview是以编程方式创建的,但我想将整个NSTextView插入到我使用接口builder创建的另一个NSScrollView中,因为您已经创建了另一个scrollview的出口。因此,只需在另一个scrollview中添加textview,就像这样[Anotherscrollview setDocumentView:theTextView]@HussainShabbir是的,但这会将整个滚动视图设置为NSTextView。我只是想让NSTextView占据我用Interface Builder创建的NSScrollView的一部分。我想知道如何将文本视图设置为scrollview的一部分。那我该怎么做呢?