Randomize Everything

Icon

And create the perfect world

Encoding HTML entities In Objective C

When sending data to a backend server you sometimes need to encode special characters to HTML entities. To encode those characters in a string use the method: stringByAddingPercentEscapesUsingEncoding. Consider the following example to make it a bit more clear:

NSString *urlString = [NSString stringWithFormat:@"%@%@", @"http://www.this-is-a-url.com/accept.php?identifier=", uniqueDeviceIdentifier];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Upload to server: %@", urlString);

Short note: the first line of the example shows you how to concatenate two strings.

Displaying Custom Fonts In Objective-C (iPhone SDK)

Coming from the Flash Platform I’m used to do a lot of font embedding in my projects. A few week ago I looked into this topic when programming an iPad application which needed a complete design makeover with custom fonts to spice it up a bit.

So how do you go about including custom fonts in an iPhone/iPad project? Well, it’s not that hard if you’re having a little bit of objective-c experience. You should download the FontLabel library, add it to your project and start creating FontLabel instances in code. I sense you feel the need for an example, so here it is:

FontLabel *questionLabel = [[FontLabel alloc] initWithFrame:CGRectMake(60, 115, 900, 130) fontName:@"DINPro-Medium" pointSize:30.0f];
questionLabel.textColor = [UIColor whiteColor];
questionLabel.textAlignment = UITextAlignmentCenter;
questionLabel.lineBreakMode = UILineBreakModeTailTruncation;
questionLabel.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
questionLabel.numberOfLines = 0;
[self.view addSubview:questionLabel];
[questionLabel release];

“DINPro-Medium” is a TrueType font included in my resources bundle. Please do mind that FontLabel currently only accepts TrueType fonts, so you might want to convert some of those non-ttf fonts to ttf. A good online font converter can be found here: http://onlinefontconverter.com/

Hit Testing A Non-Transparent Area In Objective C

Coding hit tests has always been a hassle when transparent .png files come into the game. That’s why Jeff Lamarche has extended the standard UIButton and added the hit testing of transparent .png buttons. This new class will only return a valid hit when you are touching a non-transparent part of the .png image.

More information and the downloadable source can be found here.

Some example code to get you going:

-(void)viewDidLoad
{
	UIImage *image = [UIImage imageNamed:@"image-in-resources.png"];
	IrregularShapedButton *irrButton = [[IrregularShapedButton buttonWithType:UIButtonTypeCustom] retain];
	[irrButton setImage:image forState:UIControlStateNormal];
	irrButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
 
	[self.view addSubview:irrButton];
	[super viewDidLoad];
}
 
- (IBAction)buttonClick:(id)sender
{
    NSLog(@"Clicked:%@", sender);
}