4
votes

Working on a new app, I'm running into the same problem described here in reguards to deselecting text that is input into an nstextfield: http://www.cocoabuilder.com/archive/cocoa/195313-nstextfield-how-to-deselect-text.html

There are plenty of questions around SO about doing this with NSTextViews, but I haven't been able to find a working answer for NSTextFields.

In my project, I have a window with the text field in it with a controller class that is also the text field's delegate. I have an IBAction for the text field that is sent on enter which performs actions depending on the text in the field, along with:

(void)controlTextDidChange:(NSNotification *)note

and

(BOOL)control:(NSControl *)control
       textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector 

which handle some custom autocompleting as in the answer to Suppressing the text completion dropdown for an NSTextField

My problem lies with when I hit enter to submit the inputted text, the string in the field is entirely selected, but I would like to be able to deselect the text and have the insertion point at the end, as described in the first link.

There are multiple places I could have it deselect the text, but actually doing the deselecting isn't working. I have tried obtaining the field editor as described in the first link, I have also tried using the controlTextDidEndEditing: method since I do actually get a field editor in the controlTextDidChange: method above:

- (void)controlTextDidEndEditing:(NSNotification *)note{

    NSTextView *textView = [[note userInfo] objectForKey:@"NSFieldEditor"];
    [textView setSelectedRange:NSMakeRange([[self.ParseField stringValue]length]-1, 0)
                      affinity:NSSelectionAffinityUpstream
                stillSelecting:NO];


}

I also tried disabling and reenabling editing on the field, but that didn't work either.

Something as simple as being able to send a moveDown: message to the text field would work for me, as it's be the same as hitting the down arrow, but the text field doesn't recognize that selector. (I thought NSTextField inherited from NSResponder, so it would have worked, but I guess not?)

Thank you for any help

2
maybe you should set focus to the NSTextField with [yourTextField becomeFirstResponder]; (see stackoverflow.com/questions/764179/focus-a-nstextfield)Sudo
@Sudo I just deleted my answer which worked for a textfield after I realised they are using a NSTextView not a NSTextFieldmarkhunte
@nintandrew can you look again at your question as It is not clear if you want an answer for a NSTextView or a NSTextField. I will reinstate my answer for nowmarkhunte
@Sudo I didn't need to register the textfield as first responder, but following the recommendations in that link and Markhunte's answer helped me get to an answernintandrew
@markhunte I was asking about NSTextField, thank you for your help!nintandrew

2 Answers

6
votes

In a simple app with just a window and textfield.

I set the textField to firstResponder.

This makes the text selected when the app runs.

enter image description here

If I add to the the applicationDidFinishLaunching

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

Now when run the Text is not selected and the insertion point is at the end of the text.

enter image description here

I used selectedRange to get the selection char length. As the text was selected in the first place and seem more simple.

- (void)awakeFromNib{
    [_theTextField setStringValue:@"100000"];

}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [[[NSApplication sharedApplication] mainWindow] makeFirstResponder:_theTextField];

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

}

4
votes

I was looking for the solution in Swift 3, so here is what worked for me:

theTextField.currentEditor()?.selectedRange = NSMakeRange(0, 0)