• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: selectText of NSTextField on focus
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: selectText of NSTextField on focus


  • Subject: Re: selectText of NSTextField on focus
  • From: Richard Charles <email@hidden>
  • Date: Tue, 07 Jul 2015 13:11:57 -0600

> On Jul 7, 2015, at 8:27 AM, Willeke <email@hidden> wrote:
>
>> Op 6 jul. 2015, om 18:15 heeft Richard Charles <email@hidden> het volgende geschreven:
>>
>> Does anyone have any insight into what is going on?
>>
> The animation of the focus ring isn't finished. If the focus ring is switched off, the text is selected.
>
> Safari's Address and Search field calls setSelectedRange: of currentEditor in mouseDown:
>
> - Willeke

Yes Safari’s Address and Search field has the behavior that I am looking for. First click selects the entire text field and you also get an animated focus ring.

Exactly how to achieve that on OS X 10.10 (with or without a focus ring) is another matter. I have parred back my custom NSTextField subclass to just what is shown below. A custom NSNumberFormatter is used but I removed it. The only other thing going on is the text field is programmatically placed in the super view.

====================

Your initial suggestion.

@implementation MyTextField

- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.cell setFocusRingType:NSFocusRingTypeNone];
    }
    return self;
}

- (void)awakeFromNib
{
    [self.cell setFocusRingType:NSFocusRingTypeNone];
}

- (BOOL)becomeFirstResponder
{
    BOOL result = [super becomeFirstResponder];
    if(result) {
        [self performSelector:@selector(selectText:) withObject:self afterDelay:0];
    }
    return result;
}

@end

RESULTS: Text is selected but rapidly changes to no selection. No focus ring as expected. Intermittent error Bad cursor rect event, flags = 256.

====================

You say that Safari apparently does this.

@implementation MyTextField

- (void)mouseDown:(NSEvent *)event
{
    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];

    [super mouseDown:event];
}

@end

RESULTS: Text not selected. Focus ring normal. No error message.

====================

Another version of what Safari may do.

@implementation MyTextField

- (void)mouseDown:(NSEvent *)event
{
    [self selectText:self];

    [super mouseDown:event];
}

@end

RESULTS: Text is selected but rapidly changes to no selection. Focus ring is intermittent. No error message.

====================

So in summary Safari’s Address and Search field has the behavior that I am looking for. First click selects the entire text field and you also get an animated focus ring. However on OS X 10.10 Yosemite nothing works for me except performSelector:withObject:afterDelay: using a non-zero delay value.

--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


  • Follow-Ups:
    • Re: selectText of NSTextField on focus
      • From: Willeke <email@hidden>
References: 
 >selectText of NSTextField on focus (From: Richard Charles <email@hidden>)
 >Re: selectText of NSTextField on focus (From: Willeke <email@hidden>)

  • Prev by Date: Sizing NSScrollView width to exactly fit NSTableView
  • Next by Date: Any way to combine for and if-let?
  • Previous by thread: Re: selectText of NSTextField on focus
  • Next by thread: Re: selectText of NSTextField on focus
  • Index(es):
    • Date
    • Thread