Re: NSTextField filters
Re: NSTextField filters
- Subject: Re: NSTextField filters
- From: "Louis C. Sacha" <email@hidden>
- Date: Tue, 13 Jul 2004 22:14:07 -0700
Hello...
There is one major issue that would cause problems, and a few minor things.
First, it isn't really safe to assume that the only thing that has
changed in the string is that the last character has been added.
For example, if the user moves the insertion point to the middle of
previously entered text and starts typing, your formatter will keep
checking the last character of the string instead. Also, the user can
paste in a string of text that contains multiple characters, and the
partial string validation method will only be called once with the
resulting string after the pasted text is added .
So basically, you need to check the whole string each time the
partial validation method is called.
You could simplify the process of checking a string to make sure it
contains only specific characters by using NSCharacterSet and the
NSString rangeOfCharacterFromSet:options: method.
If your formatter class always deals with a fixed character set you
could create a single instance of NSCharacterSet and store it in a
static variable, or in a more generalized implementation you could
store the character set used for that instance of the filter in an
instance variable.
The NSCharacterSet would be the set of characters that you don't want
to allow (which you can also make by inverting the character set of
the character set you want to allow).
Then, you can do
NSRange foundRange = [partialString
rangeOfCharacterFromSet:disallowedSet];
If foundRange.location is NSNotFound, then the string is acceptable.
If foundRange.location is any other value, then the string contains
one of the characters you want to filter out.
Also, returning a string by reference is newString is only useful if
you are going to return FALSE/NO. If you return TRUE/YES, it will be
ignored anyway. It is there to provide a way to return a replacement
string that will be used instead of the old entered text if you are
going to return FALSE/NO (for example if you wanted to replace
disallowed characters in an entered url with the proper escape
sequences).
Hope that helps,
Louis
Here's how I did it in a project of mine... Criticisms are welcome.
AOInputFormatter is a subclass of NSFormatter, which is instantiated
in Interface Builder and linked to the NSTextField's formatter
outlet.
============ AOInputFormatter.m ============
...
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString errorDescription:(NSString
**)error
{
char testChar;
// allow only numbers, period, slash, minus, e, E
if([partialString length] > 1)
{
testChar = [partialString characterAtIndex:([partialString
length] - 1)];
if((int)testChar < 45 || ((int)testChar > 57 &&
(int)testChar != 69 && (int)testChar != 101))
{
NSBeep();
return(NO);
}
}
*newString = partialString;
return(YES);
}
@end
================================
-- DTC
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.