Re: formatter use for whitespace
Re: formatter use for whitespace
- Subject: Re: formatter use for whitespace
- From: Bill Cheeseman <email@hidden>
- Date: Fri, 25 Apr 2003 16:17:27 -0400
on 03-04-25 2:07 PM, Theodore Petrosky at email@hidden wrote:
>
I have been working on an interface to a postgresql
>
database. part of it a client list (names, addresses,
>
etc). I want to limit an NSTextField so the user can
>
not enter a whitespace as a leading character.
>
However, whitespaces are permitted in the middle of an
>
entry.
>
>
Is this doable in a formatter or is it better to post
>
process the entry and remove any leading and double
>
spaces?
>
>
Spaces are legal characters, just not at the beginning
>
or end.
>
>
Currently, I am trimming the whitespace and manually
>
parsing the string for double spaces after the user
>
clicks to accept the entry.
It is possible to do this sort of thing in an on-the-fly formatter. It
requires implementing
isPartialStringValid:proposedSelectedRange:originalString:orignalSelectedRan
ge:errorDescripton: in your formatter in such a way that it tests whether
the insertion point is at the beginning and the character just typed is in
the whitespace character set. To be more complete, it also has to test
whether the leading characters in a pasted or dropped string at the
beginning of the text field are white space.
The following implementation will give you the basic techniques. It does not
perform the task you want, but it shows you the required techniques: (1) how
to get the pasted or dropped string (or a single typed character), and (2)
how to test it for the presence (or absence) of characters in a specified
character set, all in one mind-boggling statement.
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString
**)error {
if ([[*partialStringPtr
substringWithRange:NSMakeRange(origSelRange.location,
(*proposedSelRangePtr).location - origSelRange.location)]
rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet]
invertedSet] options:NSLiteralSearch].location != NSNotFound) {
*error = NSLocalizedString(@"Input is not a positive integer",
@"Presented when user value not a numeric digit");
return NO;
}
*error = nil;
return YES;
}
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
_______________________________________________
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.