xcode NSTextField 在自定义 NSWindow 中不可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9464423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSTextField not editable in custom NSWindow
提问by PerfectGamesOnline.com
Hi everyone,
嗨,大家好,
If I create NSTextField in my controller's view then all is fine - the field is editable. Unfortunately, I have to create NSTextField in new custom NSWindow. My code bellow produces a field which looks like without focus (text selection is gray) and is not editable (no cursor and no reaction to key strokes). I can change the text selection with mouse but that is all.
如果我在我的控制器视图中创建 NSTextField 那么一切都很好 - 该字段是可编辑的。不幸的是,我必须在新的自定义 NSWindow 中创建 NSTextField。我的代码波纹管产生一个看起来没有焦点的字段(文本选择是灰色的)并且不可编辑(没有光标并且对击键没有反应)。我可以用鼠标更改文本选择,仅此而已。
Do I have to enable the NSWindow to receive key strokes?
我是否必须启用 NSWindow 才能接收击键?
Appreciate your help, --Josef
感谢您的帮助,-- 约瑟夫
NSRect windowRect = [[self.window contentView] frame] ;
NSWindow* uiWindow = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:YES];
[uiWindow setBackgroundColor: [NSColor redColor/*clearColor*/]];
[uiWindow setOpaque:NO];
NSView* uiView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)] autorelease];
[uiView translateOriginToPoint:NSMakePoint(100, uiView.bounds.size.height/2)];
uiView.wantsLayer = YES;
[uiWindow setContentView:uiView];
NSTextField *textField;
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 80)];
[textField setFont:[NSFont fontWithName:@"Helvetica Bold" size:60]];
[textField setStringValue:@"My Label"];
[textField setBezeled:YES];
[textField setDrawsBackground:YES];
[textField setEditable:YES];
[textField setSelectable:YES];
[textField setEnabled:YES];
[uiView addSubview:textField];
// THIS DOES NOT WORK
[self.window addChildWindow:uiWindow ordered:NSWindowAbove];
// THIS WORKS
//[_graphicView addSubview:uiView];
回答by Justin Boo
You need to allow your custom window to become key window. By default, borderless windows cannot become key. In your NSWindow
subclass, add the method canBecomeKeyWindow:
:
您需要让您的自定义窗口成为关键窗口。默认情况下,无边框窗口不能成为关键。在您的NSWindow
子类中,添加方法canBecomeKeyWindow:
:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
You can check if your borderless window is key window with this:
您可以通过以下方式检查您的无边框窗口是否是关键窗口:
if([uiWindow isKeyWindow] == TRUE) {
NSLog(@"isKeyWindow!");
}
else {
NSLog(@"It's not KeyWindow!");
}
Furthermore, for a borderless window to accept key events, the class should implement acceptFirstResponder
and return YES
.
此外,对于接受关键事件的无边框窗口,该类应该实现acceptFirstResponder
并返回YES
。
回答by Manikandaraj Srinivasan
If you can just change your styleMask:NSBorderlessWindowMask to style:NSTitledWindowMask, the above Code will work. However i have also tried adding editable TextFields to NSBorderlessWindow, but it didn't seem to work for me either.
如果您可以将 styleMask:NSBorderlessWindowMask 更改为 style:NSTitledWindowMask,则上述代码将起作用。但是,我也尝试将可编辑的 TextFields 添加到 NSBorderlessWindow,但它似乎对我也不起作用。