Re: selectText of NSTextField on focus
Re: selectText of NSTextField on focus
- Subject: Re: selectText of NSTextField on focus
- From: Richard Charles <email@hidden>
- Date: Tue, 07 Jul 2015 08:26:38 -0600
> On Jul 6, 2015, at 5:49 PM, Joel Norvell <email@hidden> wrote:
>
> Hi Richard,
>
> When the instance of your NSTextField subclass becomes first responder, you have access to the NSText object (the field editor) and I believe you can use its methods to select the text. (I don't see why you can't, but since I haven't tried it myself, I'm saying "I believe.")
>
> Sincerely,
> Joel
>
> Override - (BOOL)becomeFirstResponder in your subclass.
>
> Get the field editor:
>
> NSText * itsText = [[self window] fieldEditor:YES forObject:self];
>
> Use -selectAll or -setSelectedRange
Yes I have access to the field editor for the NSTextField. Directly setting the selection using the field editor however does not work.
- (BOOL)becomeFirstResponder
{
BOOL result = [super becomeFirstResponder];
if(result) {
NSText *editor = self.currentEditor;
assert(editor && "Current editor is nil!");
assert(editor.isFieldEditor && "Editor not a field editor!");
NSRange range = NSMakeRange(0, editor.string.length);
[editor setSelectedRange:range];
}
return result;
}
The above code does not work.
There is some sort of interaction between NSTextField (and or the field editor) and the runloop that has changed for the worse in OS X 10.10 Yosemite. The only code that works is to override becomeFirstResponder and then schedule the selectText: message with the runloop to execute after a non zero delay. I have not been able to get anything else to work.
A comment by Daniel Wabyick at the end of this stackoverflow question suggests that this was a problem in OS X 10.9 Mavericks but have been unable to duplicate that.
http://stackoverflow.com/questions/2195704/selecttext-of-nstextfield-on-focus
So in summary in OS X 10.10 selecting all the text of an NSTextField when the user clicks it has become difficult.
--Richard Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden