NSTextField在自定义NSWindow中不可编辑 [英] NSTextField not editable in custom NSWindow

查看:358
本文介绍了NSTextField在自定义NSWindow中不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如果我在控制器的视图中创建NSTextField,那么一切都很好-该字段是可编辑的。
不幸的是,我必须在新的自定义NSWindow中创建NSTextField。我的代码下面会产生一个看起来没有焦点的字段(文本选择为灰色)并且不可编辑(没有光标,并且对按键没有反应)。我可以用鼠标更改文本选择,仅此而已。

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.

我必须启用NSWindow来接收按键吗?

Do I have to enable the NSWindow to receive key strokes?

感谢您的帮助,
-约瑟夫

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


推荐答案

您需要允许自定义窗口成为关键窗口。默认情况下,无边界窗口不能成为关键。在您的 NSWindow 子类中,添加方法 canBecomeKeyWindow:

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: :

- (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!");
}





此外,要使无边界窗口接受键事件,该类应实现 acceptFirstResponder 并返回 YES

Furthermore, for a borderless window to accept key events, the class should implement acceptFirstResponder and return YES.

这篇关于NSTextField在自定义NSWindow中不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆