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

ぃ、小莉子 提交于 2019-12-09 05:21:14

问题


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

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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/28779799/how-can-i-make-my-nstextfield-not-highlight-its-text-when-the-application-starts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!