Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField not editable in custom NSWindow

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.

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

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];
like image 562
PerfectGamesOnline.com Avatar asked Feb 27 '12 11:02

PerfectGamesOnline.com


2 Answers

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



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

like image 107
Justin Boo Avatar answered Nov 10 '22 00:11

Justin Boo


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.

like image 38
Manikandaraj Srinivasan Avatar answered Nov 09 '22 23:11

Manikandaraj Srinivasan