5

I've a NSView with a editable NSTextField and multiple other subviews like NSView, NSSlider, NSImage etc.

  1. After I've entered my text into the editable NSTextField and click on any of the other views, move the slider etc. I would like my NSTextField to lose focus. I've tried calling the resignFirstResponder, but that does not seems to work. How can I do this?
  2. When I mark the text inside my NSTextField a blue background behind the text is shown. How can I change the color is this?
dhrm
  • 14,335
  • 34
  • 117
  • 183
  • 2
    I think you should have asked these as 2 separate questions. How are you going to accept an answer if one person gives the best answer to (1) and another person gives the best answer to (2)? – JWWalker Nov 09 '13 at 17:29

3 Answers3

9

Suppose that you have a subclass of NSView called clickView1.h. In reference to this post, you can achieve your goal in No. 1 as follows.

- (void)mouseDown:(NSEvent *)event{
    AppDelegate *appDelegate = (AppDelegate *)[NSApp delegate];
    [appDelegate.window makeFirstResponder:nil];
}

As for No. 2, I don't understand the question.

Community
  • 1
  • 1
El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • Thanks. Ad 2) had a typo. It is the blue marking background shown when marking the text in the *NSTextField* (for instance if you would like to copy it). Is it possible to change that color? – dhrm Nov 09 '13 at 14:37
  • By saying the blue marking background, it sounds like you are referring to the focus ring. I suggest you open a new topic instead of asking two separate questions under one topic. – El Tomato Nov 09 '13 at 15:03
  • 6
    Rather than looking for an app delegate, it would seem simpler to say `[[self window] makeFirstResponder: nil]`. – JWWalker Nov 13 '13 at 15:52
  • Its nice to know about the NSApp way of doing it. Imagine if you click in another window, then self is not correct. I use self.window most of the time. – Sentry.co Feb 08 '16 at 16:01
  • 2
    self.window will return you the window, that view is bound to, not current window. So, self.window is correct, appDelegate.window is incorrect as it can be another window of your app. – Borzh May 11 '17 at 02:43
3

For question 1, I agree with BlueTomato that you need to make something else first responder, not call resignFirstResponder. For question 2, subclass NSTextFieldCell, and in the subclass, have an override like this:

- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj
{
    [super setUpFieldEditorAttributes: textObj];

    if ([textObj isKindOfClass: [NSTextView class]])
    {
        NSTextView* textView = (NSTextView*) textObj;

        [textView setSelectedTextAttributes:
            [NSDictionary dictionaryWithObjectsAndKeys:
                [NSColor redColor],
                NSBackgroundColorAttributeName,
                nil] ];
    }

    return textObj;
}
JWWalker
  • 22,385
  • 6
  • 55
  • 76
0

Try the following method:

 [[NSApp mainWindow] performSelector:@selector(resignFirstResponder:)
                          withObject:yourTextfield
                          afterDelay:0.0];
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • This fixed my issue. Thanks for the idea. To give you a bit more context here is an elaborate answer of mine from a few years back https://stackoverflow.com/a/34335928/1041122 – Julian F. Weinert Jan 19 '21 at 02:49