Re: Controlling selection in NSTextFields
Re: Controlling selection in NSTextFields
- Subject: Re: Controlling selection in NSTextFields
- From: "Erik M. Buck" <email@hidden>
- Date: Sat, 10 Nov 2001 17:56:55 -0600
>
Well, the embedded NSText object is damnably difficult to get ahold
>
of, but I succeeded after subclassing both NSTextField AND
>
NSTextFieldCell and capturing the NSText object in some critical call
>
- but the process struck me as extraordinarily kludgy and cumbersome,
>
especially as IB seems to have no provision to change a field's cell
>
class.
[[myTextField window] fieldEditor:YES forObject:myTextField ]; // this is
not damnably difficult
The Field Editor
Each NSWindow has a text object that is shared for light editing tasks. This
object, the window's field editor, is inserted in the view hierarchy when an
object needs to edit some text and removed when the object is finished. The
field editor is used by NSTextFields and other controls, for example, to
edit the text that they display. The fieldEditor:forObject: method returns a
window's field editor, after asking the delegate for a substitute using
windowWillReturnFieldEditor:toObject:. You can override the NSWindow method
in subclasses or provide a delegate to substitute a class of text object
different from the default of NSTextView, thereby customizing text editing
in your application.
There is also NSControl's
currentEditor
- (NSText *)currentEditor
If the receiver is being edited-that is, it has an NSText object acting as
its field editor, and is the first responder of its NSWindow-this method
returns the NSText editor; otherwise, it returns nil.
>
>
Anyway, it turns out that the delegate's
>
textView:willChangeSelectionFromCharacterRange:toCharacterRange: is
>
indeed called when tabbing into the field, and in several other
>
circumstances, but NEVER in the case which I thought logical and most
>
important: when the insertion point or selection is modified with the
>
mouse.
>
>
So, I'm stuck. Is there any way of intercepting mouse
>
selection/insertion point movement while it happens, or will I be
>
forced to reimplement my own NSTextField (and NSTextFieldCell)
>
completely from scratch? Some other methods come to mind but are
>
equally distasteful.
>
I like
- (BOOL)textShouldBeginEditing:(NSText *)aTextObject
and/or
- (void)textDidBeginEditing:(NSNotification *)aNotification
and/or
acceptsFirstResponder
- (BOOL)acceptsFirstResponder
Overridden by subclasses to return YES if the receiver can handle key events
and action messages sent up the responder chain. NSResponder's
implementation returns NO, indicating that by default a responder object
doesn't agree to become first responder. Objects that aren't first responder
can receive mouse event messages, but no other event or action messages.
See Also: - becomeFirstResponder - resignFirstResponder -
needsPanelToBecomeKey (NSView)
You could override acceptsFirstResponder or becomeFirstResponder to get what
you want.