NSTextField not editable in custom NSWindow

Hello to all,

If I create an NSTextField in the controller view, everything will be fine - the field is editable. Unfortunately, I need to create an NSTextField in a new custom NSWindow. My code below gives a field that looks without focus (text selection is gray) and cannot be edited (no cursor and no reaction to keystrokes). I can change the text selection with the mouse, but that’s all.

Should I enable NSWindow to receive keystrokes?

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]; 
+7
source share
2 answers

You need your custom window to become the key window. By default, borderless windows cannot become key. In a subclass of NSWindow add the canBecomeKeyWindow: method:

 - (BOOL)canBecomeKeyWindow { return YES; } 



You can check if your borderless window is the key window with this:

 if([uiWindow isKeyWindow] == TRUE) { NSLog(@"isKeyWindow!"); } else { NSLog(@"It not KeyWindow!"); } 


<h / ">

In addition, for the borderless window to accept key events, the class must implement acceptFirstResponder and return YES .

+10
source

If you can just change your Style: Mask: NSBorderlessWindowMask to: NSTitledWindowMask, the above code will work. However, I also tried adding editable TextFields to NSBorderlessWindow, but it didn't seem to work for me either.

+1
source

All Articles