简体   繁体   English

如何将NSTextView添加到NSScrollView?

[英]How do I add an NSTextView to an NSScrollView?

This document . 这个文件 explains how to programmatically add an NSTextview (that can scroll) to a window. 解释了如何以编程方式将NSTextview(可以滚动)添加到窗口。 However, how do I programmatically add an NSTextView into an NSScrollView? 但是,如何以编程方式将NSTextView添加到NSScrollView中?

EDIT: Here's is the code that works with windows. 编辑:这是与Windows一起工作的代码。 I want to be able to programmatically insert an NSTextView into a NSScrollView I created with Interface Builder 我希望能够以编程方式将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];

As pointed out in the comments you can achieve an NSTextView within the scroll view by setting it as document view. 如注释中所指出的,您可以通过将其设置为文档视图来在滚动视图中实现NSTextView。 If you want the textview only to take up a small part of the scroll view you have to construct that in another way. 如果只希望textview占据滚动视图的一小部分,则必须以另一种方式构造它。 What is the goal you want to achieve? 您要实现的目标是什么?

I also had issues with programmatic creation of scrollable NSTextView. 我还遇到了以编程方式创建可滚动NSTextView的问题。 Based on my attempts I have created working example app: How to create scrollable NSTextView programmatically? 基于我的尝试,我创建了工作示例应用程序: 如何以编程方式创建可滚动的NSTextView? .

Here is relevant code: 以下是相关代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM