How can I make my NSTextField NOT highlight its text when the application starts?
Asked Answered
D

3

14

When my application launches, the first NSTextField is being selected like this:

enter image description here

I can edit the NSTextField fine, but when I press enter to end the editing, the text becomes selected again, and the editing does not end.

I followed the Apple tutorial here, and I had the same problem with the text field being perpetually highlighted.

How do I stop this? I would like it so the text field is not the first responder of the app so it's not edited right away, and when it is being edited, clicking outside of the text field will end it. I'm not sure where to put the [[textField window]makeFirstResponder:nil] to stop the editing in the latter case.

I'm running Yosemite 10.10.2.

Diann answered 28/2, 2015 at 8:42 Comment(2)
Hi! I may have a solution that will work. Do you want to be able to tab to the text field?Dandelion
Is there an example of this in swift?Hundredth
D
18

Your text field is selecting the text, due to the default implementation of becomeFirstResponder in NSTextField.

To prevent selection, subclass NSTextField, and override becomeFirstResponder to deselect any text:

- (BOOL) becomeFirstResponder
{
    BOOL responderStatus = [super becomeFirstResponder];

    NSRange selectionRange = [[self  currentEditor] selectedRange];
    [[self currentEditor] setSelectedRange:NSMakeRange(selectionRange.length,0)];

    return responderStatus;
}

The resulting behavior is that the field does not select the text when it gets the focus.

To make nothing the first responder, call makeFirstResponder:nil after your application finishes launching. I like to subclass NSObject to define doInitWithContentView:(NSView *)contentView, and call it from my NSApplicationDelegate. In the code below, _window is an IBOutlet:

- (void) applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    [_menuController doInitWithContentView:_window.contentView];
}

The reason your field is getting focus when the application starts is because the window automatically gives focus to the first control. It determines what is considered first, by scanning left to right, top down (it scans left to right first, since a text field placed at the top right will still get focused). One caveat is that if the window is restorable, and you terminate the application from within Xcode, then whatever field was last focused will retain the focus state from the last execution.

Dandelion answered 3/9, 2015 at 16:5 Comment(8)
I need the text field to still select text, so overriding the becomeFirstResponder is not a solution. I actually had already tried the method of calling makeFirstResponder:nil, but that does not work either. I also made sure that my window is -not- restorable, but that does not seem to be part of the problem.Scavenge
When exactly do you need the text to be selected? Can you describe the behavior that you need with more detail?Dandelion
If the only time you don't want it selected is when the application starts, then perhaps all you need to do is call makeFirstResponder: from a different place. I tried it with a very simple example and it worked for me. Did you try calling it from applicationDidFinishLaunching:?Dandelion
I tried calling it from awakeFromNib of the window controller that is having the issue, which should happen after applicationDidFinishLaunchingScavenge
I think that is your problem right there. awakeFromNib gave me issues. I now do all UI initialization from applicationDidFinishLaunching, or later.Dandelion
My awakeFromNib is called AFTER applicationDidFinishLaunching so I don't think that is my problem.Scavenge
What happens on your end when you create simple project with a window that has one text field, and run the application? I'm on Yosemite, and I can set the focus to nil, by doing initialization in applicationDidFinishLaunchingDandelion
If we get different results, then there is something else that is happening, which is re-setting the focus.Dandelion
P
5

I am using IB, there's a property on NSTextField called Refuses First Responder. Ticking that will prevent the highlighting of the text field immediately after the window is presented. There's some more detailed info about Refuses First Responder in this question.

Pea answered 9/5, 2017 at 9:36 Comment(0)
I
0

No need to subclass. Simply set refusesFirstResponder = YES;

NSTextField *textField = [NSTextField new];
textField.refusesFirstResponder = YES;

That's it! Do that and it won't highlight the text in the field.

Intercut answered 27/10, 2019 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.