Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselecting Text in NSTextField

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

like image 431
nintandrew Avatar asked Dec 25 '22 22:12

nintandrew


1 Answers

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)];

}

like image 120
markhunte Avatar answered Dec 28 '22 09:12

markhunte