Re: NSTextField, maximum string length, bindings and NSFormatter
Re: NSTextField, maximum string length, bindings and NSFormatter
- Subject: Re: NSTextField, maximum string length, bindings and NSFormatter
- From: Dmitriy Balakirev <email@hidden>
- Date: Thu, 24 Oct 2013 19:41:15 +0400
For limiting text in NSTextField this works well: (_limit == 5, 30, etc.)
- (BOOL)isPartialStringValid:(NSString *__autoreleasing *)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString *__autoreleasing *)error {
NSString *partialStr = *partialStringPtr;
if ([partialStr length]) {
if ([partialStr length] <= _limit) {
*partialStringPtr = [partialStr uppercaseString];
return NO;
}
*partialStringPtr = [NSString stringWithString:origString];
*proposedSelRangePtr = NSMakeRange(origSelRange.location, origSelRange.length);
return NO;
} else {
return YES;
}
}
Want only certain char? Do that:
- (NSCharacterSet *)forbiddenSet {
if (!_forbiddenSet) {
NSString *alphaStr = @"qwertyuiopasdfghjklzxcvbnm";
NSMutableCharacterSet *allowedSet = [NSMutableCharacterSet characterSetWithCharactersInString:alphaStr];
[allowedSet addCharactersInString:[alphaStr uppercaseString]];
_forbiddenSet = [allowedSet invertedSet];
}
return _forbiddenSet;
}
- (BOOL)isPartialStringValid:(NSString *__autoreleasing *)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString *__autoreleasing *)error {
NSString *partialStr = *partialStringPtr;
if ([partialStr length]) {
NSRange rangeOfForbiddenChar = [partialStr rangeOfCharacterFromSet:[self forbiddenSet]];
if ((rangeOfForbiddenChar.location == NSNotFound) && ([partialStr length] <= _limit)) {
*partialStringPtr = [partialStr uppercaseString];
return NO;
}
*partialStringPtr = [NSString stringWithString:origString];
*proposedSelRangePtr = NSMakeRange(origSelRange.location, origSelRange.length);
return NO;
} else {
return YES;
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden