Creating NSTextView Software?

I cannot create a working NSTextView programmatically. Starting with the new project template, I have this code (mainly from Apple's โ€œText System UI Programming Guideโ€):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSString *string = @"Here to the ones who see things different.";
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:string];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    [textStorage addLayoutManager:layoutManager];

    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(300, 300)];
    [layoutManager addTextContainer:textContainer];

    NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(50, 50, 300, 300)
                                               textContainer:textContainer];

    [textContainer setWidthTracksTextView:YES];
    [textView setVerticallyResizable:YES];
    [textView setHorizontallyResizable:NO];
    [textView setAutoresizingMask:NSViewWidthSizable];

    [[self.window contentView] addSubview:textView];
}

Running the application opens a window with a white square in it, which should be an NSTextView, but there is no text and there is no way to interact with the text representation. Moreover, I tried to add text to the text view in the code after creating the view with [textView setString:@"Some string."], but this will not work either. (By the way, I also tried first migrating it to NSScrollView if this was somehow a problem, but that didn't help.)

I canโ€™t understand in my life what the problem is. What am I missing?

+4
2

, . , , 100%. , , initWithFrame: ( ).

NSTextView *textView = [[NSTextView alloc] initWithFrame:NSMakeRect(50, 50, 300, 300)];
NSLayoutManager *layoutManager = textView.layoutManager;
NSTextContainer *textContainer = textView.textContainer;
NSTextStorage *textStorage = textView.textStorage;

//Do your edits here
0

TheAmateurProgrammer:

, , initWithFrame: ( )

iOS , UITextView . NSTextView, . , OP , , , .

0

Source: https://habr.com/ru/post/1548205/


All Articles