Restrict NSTextField to only allow numbers

Try to make your own NSNumberFormatter subclass and check the input value in -isPartialStringValid:newEditingString:errorDescription: method. @interface OnlyIntegerValueFormatter : NSNumberFormatter @end @implementation OnlyIntegerValueFormatter – (BOOL)isPartialStringValid:(NSString*)partialString newEditingString:(NSString**)newString errorDescription:(NSString**)error { if([partialString length] == 0) { return YES; } NSScanner* scanner = [NSScanner scannerWithString:partialString]; if(!([scanner scanInt:0] && [scanner isAtEnd])) { NSBeep(); return NO; } return YES; } @end And … Read more

NSTextField – White text on black background, but black cursor

Since in practice the NSText* returned by -currentEditor for an NSTextField is always an NSTextView*, I added the following code to my custom NSTextField subclass: -(BOOL) becomeFirstResponder { BOOL success = [super becomeFirstResponder]; if( success ) { // Strictly spoken, NSText (which currentEditor returns) doesn’t // implement setInsertionPointColor:, but it’s an NSTextView in practice. // … Read more

Get text of button from IBAction – iPhone

The sender should be the control which initiated the action. However, you should not assume its type and should instead leave it defined as an id. Instead, check for the object’s class in the actual method as follows: – (IBAction)onClick1:(id)sender { // Make sure it’s a UIButton if (![sender isKindOfClass:[UIButton class]]) return; NSString *title = … Read more

Easiest way to format a number with thousand separators to an NSString according to the Locale

For 10.6 this works: NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setFormatterBehavior: NSNumberFormatterBehavior10_4]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; NSString *numberString = [numberFormatter stringFromNumber: [NSNumber numberWithInteger: i]]; And it properly handles localization.

Faking Subpixel Antialiasing on Text with Core Animation

If you’re using a transparent sheet, you don’t know in advance what the pixels below it will be. They may change. Remember that you have a single alpha channel for all three colors: if you make it transparent, you won’t see any subpixel effect, but if you make it opaque, all three subelements are going … Read more

What is the correct way to handle stale NSURL bookmarks?

After a lot of disappointing testing I’ve come to the following conclusions. Though logical, they’re disappointing since the resulting experience for users is far from ideal and a significant pain for developers depending on how far they’re willing to go to help users re-establish references to bookmarked resources. When I say “renew” below, I mean … Read more