• 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
Controlling selection in NSTextFields
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Controlling selection in NSTextFields


  • Subject: Controlling selection in NSTextFields
  • From: Rainer Brockerhoff <email@hidden>
  • Date: Sat, 10 Nov 2001 20:42:06 -0200

Hi folks,

I have a couple of NSTextFields with special formatting. In particular, I'm inserting spaces at some points of the field contents, which are fixed-length.

So I've defined an NSFormatter to insert the spaces when showing the value, remove them to get the edited value, and all that sort of thing which is working well.

Now I'm stuck on the very last part. I'm using -[NSFormatter isPartialStringValid: proposedSelectedRange: originalString: originalSelectedRange: errorDescription:] (whew!) to keep the formatting looking OK while typing over the field, and to avoid selecting the inserted spaces.

This also works quite well; however, I could figure out no way to do this when the user first clicks on the field! The selector above is not called in that case. Neither, it seems, is anything else.

After RTFM and so forth, I decided that the best way would be to implement textView:willChangeSelectionFromCharacterRange:toCharacterRange: in my field's delegate. However, it turns out that this is never called. Some digging revealed the fact that this has to be set for the NSTextField's NSTextFieldCell's embedded NSText object, whose delegate is normally nil.

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.

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 can't believe nobody else needs to do this!

TIA,

--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"Originality is the art of concealing your sources."
http://www.brockerhoff.net/ (updated Oct. 2001)


  • Follow-Ups:
    • Re: Controlling selection in NSTextFields
      • From: "Erik M. Buck" <email@hidden>
    • Re: Controlling selection in NSTextFields
      • From: "Sven A. Schmidt" <email@hidden>
  • Prev by Date: Re: New Cocoa Programmer
  • Next by Date: Re: Threading, save panels, and segfaults
  • Previous by thread: Re: Newbie: how to use custom views
  • Next by thread: Re: Controlling selection in NSTextFields
  • Index(es):
    • Date
    • Thread