NSTextField: How to DEselect text?

  • I'd like to pre-place some text into an NSTextField, and then give it
    focus (make it firstResponder) and immediately allow the user type in
    additional text, concatenating it.

    Unfortunately, when I make the field firstResponder, my pre-placed
    text is selected, and when the user starts typing, it gets
    overwritten.  Very annoying.

    Does anyone know how to deselect text in an NSTextField?

    NSTextField has a selectText:(id)sender method but no deselectText:
    method.

    I've tried sending -setStringValue:, -makeFirstResponder: and -
    endEditingFor: in various different orders and combinations, but
    nothing works.

    Thanks,

    Jerry Krinock
  • Look into the field editor. You probably need to manipulate the
    selection on the field editor, not the NSTextField itself.

    Jerry Krinock wrote:
    > I'd like to pre-place some text into an NSTextField, and then give it
    > focus (make it firstResponder) and immediately allow the user type in
    > additional text, concatenating it.
    >
    > Unfortunately, when I make the field firstResponder, my pre-placed
    > text is selected, and when the user starts typing, it gets
    > overwritten.  Very annoying.
    >
    > Does anyone know how to deselect text in an NSTextField?
    >
    > NSTextField has a selectText:(id)sender method but no deselectText:
    > method.
    >
    > I've tried sending -setStringValue:, -makeFirstResponder: and
    > -endEditingFor: in various different orders and combinations, but
    > nothing works.
    >
    > Thanks,
    >
    > Jerry Krinock
  • Hi Jerry

    You need to get hold of the field editor associated with the text
    field in
    the awakeFromNib of your controller and configure it. The code could be
    something like:
    =====
    -(void)awakeFromNib
    {
    [mTextField setSelectable:YES];
    [mTextField setDelegate:self];
    [mWindow makeFirstResponder:mTextField];

    //Get hold of the field editor and deselect its text
    NSText* fieldEditor = [mWindow fieldEditor:YES forObject:mTextField];
    [fieldEditor setSelectedRange:NSMakeRange([[fieldEditor string]
    length],0)];
    [fieldEditor setNeedsDisplay:YES];
    }
    =====

    Hope this helps.

    Regards
    Shripada

    > I'd like to pre-place some text into an NSTextField, and then give it
    > focus (make it firstResponder) and immediately allow the user type in
    > additional text, concatenating it.
    >
    > Unfortunately, when I make the field firstResponder, my pre-placed
    > text is selected, and when the user starts typing, it gets
    > overwritten.  Very annoying.
    >
    > Does anyone know how to deselect text in an NSTextField?
    >
    > NSTextField has a selectText:(id)sender method but no deselectText:
    > method.
    >
    > I've tried sending -setStringValue:, -makeFirstResponder: and -
    > endEditingFor: in various different orders and combinations, but
    > nothing works.
    >
    > Thanks,
    >
    > Jerry Krinock
    >
  • On 2007 Dec, 20, at 22:12, Shripada Hebbar wrote:

    > [mWindow makeFirstResponder:mTextField];
    >
    > //Get hold of the field editor and deselect its text
    > NSText* fieldEditor = [mWindow fieldEditor:YES forObject:mTextField];
    > [fieldEditor setSelectedRange:NSMakeRange([[fieldEditor string]
    > length],0)];
    > [fieldEditor setNeedsDisplay:YES];

    Thank you, Shripada.  I just figured this out after considering John's
    reply.

    The location member of the range passed to setSelectedRange should be
    set to the end of the string, as you have done.  At first I set the
    range to {0,0}, but that put the insertion point at the ^beginning^ of
    the text, which was another ARGHHHH, because there ain't no -
    setInsertionPoint in any of the related classes.

    Apparently, the insertion point is always set to the ^end^ of the
    selectedRange.

    All works now.  Thanks.
  • Actually, an insertion point is just a zero-length selection.

    Jerry Krinock wrote:
    >
    > On 2007 Dec, 20, at 22:12, Shripada Hebbar wrote:
    >
    >> [mWindow makeFirstResponder:mTextField];
    >>
    >> //Get hold of the field editor and deselect its text
    >> NSText* fieldEditor = [mWindow fieldEditor:YES forObject:mTextField];
    >> [fieldEditor setSelectedRange:NSMakeRange([[fieldEditor string]
    >> length],0)];
    >> [fieldEditor setNeedsDisplay:YES];
    >
    > Thank you, Shripada.  I just figured this out after considering John's
    > reply.
    >
    > The location member of the range passed to setSelectedRange should be
    > set to the end of the string, as you have done.  At first I set the
    > range to {0,0}, but that put the insertion point at the ^beginning^ of
    > the text, which was another ARGHHHH, because there ain't no
    > -setInsertionPoint in any of the related classes.
    >
    > Apparently, the insertion point is always set to the ^end^ of the
    > selectedRange.
    >
    > All works now.  Thanks.
  • On Dec 21, 2007 1:31 AM, Jerry Krinock <jerry...> wrote:
    >
    > On 2007 Dec, 20, at 22:12, Shripada Hebbar wrote:
    >
    >> [mWindow makeFirstResponder:mTextField];
    >>
    >> //Get hold of the field editor and deselect its text
    >> NSText* fieldEditor = [mWindow fieldEditor:YES forObject:mTextField];
    >> [fieldEditor setSelectedRange:NSMakeRange([[fieldEditor string]
    >> length],0)];
    >> [fieldEditor setNeedsDisplay:YES];
    >
    > Thank you, Shripada.  I just figured this out after considering John's
    > reply.
    >
    > The location member of the range passed to setSelectedRange should be
    > set to the end of the string, as you have done.  At first I set the
    > range to {0,0}, but that put the insertion point at the ^beginning^ of
    > the text, which was another ARGHHHH, because there ain't no -
    > setInsertionPoint in any of the related classes.
    >
    > Apparently, the insertion point is always set to the ^end^ of the
    > selectedRange.

    No, the selected range and the insertion point are the same thing. If
    it has zero length, it shows up as a blinking vertical line, and if it
    doesn't, then it appears as a highlighted range of text.

    --
    Clark S. Cox III
    <clarkcox3...>
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today