594Delaying Methods. And cancelling the delaying of methods

Delaying a method should be by now fairly clear:

[self performSelector:@selector(myMethod) withObject:nil afterDelay:3];

But what, if you need to cancel the delayed perform request? That’s the way to do it:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(myMethod) object:nil];

cancelPreviousPerformRequestsWithTarget‘ seems to be the current leader in the ‘Longest Objective C Method Name Competition” with 39 characters, not bad.

Let me know, if you find any more contenders…

591Fonts on the iPhone

I never seen to be able to remember the fonts available on the iPhone. Here’s the code to print out all font families and font styles.

for (NSString *familyName in [UIFont familyNames]) {
	NSLog(@"%@, %@", familyName, [UIFont fontNamesForFamilyName:familyName]);
}

And here is the current result:

2010-05-17 17:38:05.931 fonts[20843:207] AppleGothic, (
    AppleGothic
)
2010-05-17 17:38:05.932 fonts[20843:207] Hiragino Kaku Gothic ProN, (
    "HiraKakuProN-W6",
    "HiraKakuProN-W3"
)
2010-05-17 17:38:05.932 fonts[20843:207] Arial Unicode MS, (
    ArialUnicodeMS
)
2010-05-17 17:38:05.932 fonts[20843:207] Heiti K, (
    "STHeitiK-Medium",
    "STHeitiK-Light"
)
2010-05-17 17:38:05.932 fonts[20843:207] DB LCD Temp, (
    DBLCDTempBlack
)
2010-05-17 17:38:05.932 fonts[20843:207] Helvetica, (
    "Helvetica-Oblique",
    "Helvetica-BoldOblique",
    Helvetica,
    "Helvetica-Bold"
)
2010-05-17 17:38:05.932 fonts[20843:207] Marker Felt, (
    "MarkerFelt-Thin"
)
2010-05-17 17:38:05.933 fonts[20843:207] Times New Roman, (
    TimesNewRomanPSMT,
    "TimesNewRomanPS-BoldMT",
    "TimesNewRomanPS-BoldItalicMT",
    "TimesNewRomanPS-ItalicMT"
)
2010-05-17 17:38:05.933 fonts[20843:207] Verdana, (
    "Verdana-Bold",
    "Verdana-BoldItalic",
    Verdana,
    "Verdana-Italic"
)
2010-05-17 17:38:05.933 fonts[20843:207] Georgia, (
    "Georgia-Bold",
    Georgia,
    "Georgia-BoldItalic",
    "Georgia-Italic"
)
2010-05-17 17:38:05.933 fonts[20843:207] Arial Rounded MT Bold, (
    ArialRoundedMTBold
)
2010-05-17 17:38:05.933 fonts[20843:207] Trebuchet MS, (
    "TrebuchetMS-Italic",
    TrebuchetMS,
    "Trebuchet-BoldItalic",
    "TrebuchetMS-Bold"
)
2010-05-17 17:38:05.933 fonts[20843:207] Heiti TC, (
    "STHeitiTC-Light",
    "STHeitiTC-Medium"
)
2010-05-17 17:38:05.933 fonts[20843:207] Geeza Pro, (
    "GeezaPro-Bold",
    GeezaPro
)
2010-05-17 17:38:05.934 fonts[20843:207] Courier, (
    Courier,
    "Courier-BoldOblique",
    "Courier-Oblique",
    "Courier-Bold"
)
2010-05-17 17:38:05.934 fonts[20843:207] Arial, (
    ArialMT,
    "Arial-BoldMT",
    "Arial-BoldItalicMT",
    "Arial-ItalicMT"
)
2010-05-17 17:38:05.934 fonts[20843:207] Heiti J, (
    "STHeitiJ-Medium",
    "STHeitiJ-Light"
)
2010-05-17 17:38:05.934 fonts[20843:207] Arial Hebrew, (
    ArialHebrew,
    "ArialHebrew-Bold"
)
2010-05-17 17:38:05.934 fonts[20843:207] Courier New, (
    "CourierNewPS-BoldMT",
    "CourierNewPS-ItalicMT",
    "CourierNewPS-BoldItalicMT",
    CourierNewPSMT
)
2010-05-17 17:38:05.934 fonts[20843:207] Zapfino, (
    Zapfino
)
2010-05-17 17:38:05.934 fonts[20843:207] American Typewriter, (
    AmericanTypewriter,
    "AmericanTypewriter-Bold"
)
2010-05-17 17:38:05.935 fonts[20843:207] Heiti SC, (
    "STHeitiSC-Medium",
    "STHeitiSC-Light"
)
2010-05-17 17:38:05.935 fonts[20843:207] Helvetica Neue, (
    HelveticaNeue,
    "HelveticaNeue-Bold"
)
2010-05-17 17:38:05.935 fonts[20843:207] Thonburi, (
    "Thonburi-Bold",
    Thonburi
)

590Adding an UIAlertView

Unlike other UIViews, UIAlertView does not need to be added to another view via addSubView.
[myAlertView show] takes care of that.

Trivial, maybe. But I wasn’t aware of it.

583UIAlertViews additional Buttons

UIAlertView *alert = [[UIAlertView alloc]
	 initWithTitle:@"Hello"
	 message:@"Do you really want to?"
	 delegate:self
	 cancelButtonTitle:@"Cancel" 
	 otherButtonTitles:@"OK", nil];

The intuively unobvious thing is, that otherButtonTitles requires a nil-terminated, comma-seperated list(?) of NSStrings.

Although you can add (any number?) of additional buttons, it gets silly after about 3.

Another thing to note is that in the case of two buttons, they get displayed side by side, whereas one button or more than two are shown vertically stacked.

581Resizing an UIImage

Nice and simple Category to resize an UIImage

@interface UIImage (Resize)
 
- (UIImage*)scaleToSize:(CGSize)size;
 
@end
#import "UIImageResize.h"
#import <CoreGraphics/CoreGraphics.h>
 
@implementation UIImage (Resize)
 
- (UIImage*)scaleToSize:(CGSize)size {
 
	UIGraphicsBeginImageContext(size);
 
	[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
 
	UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
 
	UIGraphicsEndImageContext();
 
	return scaledImage;
}
 
@end