• 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
Re: NSTextField filters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


References: 
 >NSTextField filters (From: Bret Kurth <email@hidden>)
 >Re: NSTextField filters (From: Shawn Erickson <email@hidden>)
 >Re: NSTextField filters (From: Daniel Todd Currie <email@hidden>)

  • Prev by Date: Re: NSTableView delegate not getting called
  • Next by Date: RE: Makefile
  • Previous by thread: Re: NSTextField filters
  • Next by thread: How to disable the "Open Recent" menu
  • Index(es):
    • Date
    • Thread