Text Field Limits and Undo
Text Field Limits and Undo
- Subject: Text Field Limits and Undo
- From: Mike Rossetti <email@hidden>
- Date: Wed, 10 Aug 2005 09:46:13 -0600
Friends,
IB, unfortunately, does not provide a built-in mechanism for specifying
a maximum number of characters allowed to be entered into an
NSTextField. The app I'm working on has many windows with hundreds of
fields for which unique limits (i.e. they're not all the same) need to
be applied.
There are a limited number of ways to apply maximum number of character
limits:
- Make a custom version of NSTextField and have each of the target
fields derive therefrom. This approach would require the unique limit
for each field to be set programatically. Hmm. Lots of code/work.
- Use a custom NSFormatter. I can't seem to find a way to get IB to
allow me to add a custom formatter to an NSTextField. Even if I could,
I'm not sure I could set the limit within IB. Is there a way to extend
IB to support this kind of thing? This would really be the best
approach.
- Hack it. Hey! That's what I've done! (But this exposes a bug.)
Here's how I do it:
1. Set the delegate of each field to be the window controller (a custom
window controller).
2. Added a controlTextDidChange: function to the window controller.
3. Upon a change to the text, controlTextDidChange: checks to see if
the 'tag' for the NSTextField is negative and, if so, figures that this
is the negative of the character count limit.
4. If the string of the NSTextField exceeds the limit then beep and
truncate the string.
It works!
But there's a problem.
If one types too much and gets the beep/string truncation AND THEN DOES
AN UNDO --> assert down after NSUndoTyping/undoRedo with a range check
in NSBigMutableString substringWithRange.
Moral of the story: Don't muck with the string while in
controlTextDidChange.
Mike
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden