NSTextView - 无法编辑文本



我正在使用如下代码在OSX应用程序中创建一些NSTextView对象:

NSTextView *testTextView = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 700, 1200, 100)];
[testTextView setFont:[NSFont fontWithName:@"HelveticaNeue-Light" size:18]];
[testTextView setDelegate:self];
[testTextView setBackgroundColor:[NSColor lightGrayColor]];
[testTextView setString:@"Some text to populate this lovely NSTextView. I would like to be able to edit this text but I can't."];
[testTextView setEditable:TRUE];
[[self.window contentView] addSubview:testTextView];
[self.window makeFirstResponder:testTextView];

NSTextView在屏幕上正确显示,文本可见,但我无法编辑文本。

谁能指出我做错了什么。

多谢

如果您使用的是无标题窗口,则无法编辑的对象,如NsTextField,NsTextView......。

如果您使用的是无标题窗口,该窗口必须能够成为密钥窗口,您需要创建 NSWindow 的子类并覆盖 -canBecomeKeyWindow,如下所示:

-(BOOL)canBecomeKeyWindow {
    return YES;
}

最新更新