How to set up an NSTextView programmatically with explicit NSLayoutManager, NSTextStorage, NSTextContainer?

后端 未结 2 1960
感动是毒
感动是毒 2021-02-06 05:29

Following the apple documentation I am trying to set up a simple NSTextView via its two constructor methods.

I am placing the below code inside the v

2条回答
  •  野性不改
    2021-02-06 06:00

    #import 
    
    @interface TextViewController : NSObject {
    
        NSLayoutManager *secondLayout;
    
        IBOutlet NSSplitView *columnView;
        IBOutlet NSTextView *bottomView;
    
    }
    
    - (IBAction) addColumn: (id)sender;
    
    @end
    #import "TextViewController.h"
    
    @implementation TextViewController
    
    - (void)awakeFromNib
    {
        NSTextStorage *storage = [bottomView textStorage];
        secondLayout = [NSLayoutManager new];
        [storage addLayoutManager: secondLayout];
        [secondLayout release];
        [self addColumn: nil];
        [self addColumn: nil];
    }
    
    
    - (IBAction) addColumn: (id)sender
    {
        NSRect frame = [columnView frame];
    
        NSTextContainer *container = [[NSTextContainer alloc]
                                      initWithContainerSize: frame.size];
        [container setHeightTracksTextView: YES];
        [container setWidthTracksTextView: YES];
    
        [secondLayout addTextContainer: container];
        [container release];
        NSTextView *newView = [[NSTextView alloc] initWithFrame: frame
                                                  textContainer: container];
        [columnView addSubview: newView];
        [newView release];
    }
    
    @end
    

提交回复
热议问题