Tag Archives: Objective-C

iPhone – Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4

A few months ago, i have a post talking about adding a return button for UIKeyboardTypeNumberPad.
iPhone – Add the Return Key for UIKeyboardTypeNumberPad

But that only works in iOS SDK <= 3.2M. After i upgrade it to 4.0, that feature was broken.

It is found that the we have add the observer to the UIKeyboardDidShowNotification instead of UIKeyboardWillShowNotification. Moreover, the view prefix is changed from <UIKeyboard to <UIPeripheralHostView. Here comes the code. Continue reading iPhone – Add the Return Key for UIKeyboardTypeNumberPad in iPhone SDK 4

iPod Touch – Check if Camera is Available

Previously my first iPhone Application was rejected by Apple App Store. That application has a feature which allow users to take a photo or select an existing photo in the gallery.

As iPod Touch does not have a camera, the application crashes when user decide to take a photo. That’s why App Store rejected it.

So i have to check if the camera of the device is available. The following piece of code can do. Continue reading iPod Touch – Check if Camera is Available

Objective-C – Rounding float numbers

In Objective-C, you can use the ceil(), floor() and lroundf() functions to round a float number.

// ceil()
NSLog(@&quot;ceil(1.4f)   : %f&quot;, ceil(1.4f));
NSLog(@&quot;ceil(1.5f)   : %f&quot;, ceil(1.5f));

// floor()
NSLog(@&quot;floor(1.5f)  : %f&quot;, floor(1.5f));
NSLog(@&quot;floor(1.5f)  : %f&quot;, floor(1.5f));

// lroundf()
NSLog(@&quot;lroundf(1.4f): %d&quot;, lroundf(1.4f));
NSLog(@&quot;lroundf(1.5f): %d&quot;, lroundf(1.5f));

Continue reading Objective-C – Rounding float numbers

Objective-C – NSDictionary Example

NSDictionary is a useful object to store key-pair values just like HashMap in Java.

You can create and iterate a NSDictionary as follow

// Create a NSDictionary
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil];
NSArray *objs = [NSArray arrayWithObjects:@"obj1", @"obj2", @"obj3", nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
	
// Iterate it
for (id key in dict) {
	NSLog(@"key: %@   value:%@", key, [dict objectForKey:key]);
}

  Continue reading Objective-C – NSDictionary Example

iPhone – Wrap Text in UILabel

In UILabel, there is an adjustsFontSizeToFitWidth property which will automatically adjust the text font size to fit the UILabel width.

UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 20.0)];
aLabel.adjustsFontSizetoWidth = YES; // This is a must
aLabel.minimumFontSize = 8.0f; 
aLabel.numberOfLines = 1; // This is a must too =.=

But this only work for numberOfLines = 1Continue reading iPhone – Wrap Text in UILabel

iPhone – Localization

Localization is a must in mobile application development. The article will shows you how to localize your iPhone Application in just a few steps.

1. Whenever you need a localized string in your application, use the NSLocalizedString(NSString *key, NSString *comment).

[self.label1 setText:NSLocalizedString(@"label1.text", @"Label1 in MyViewController.m")];
[self.label2 setText:NSLocalizedString(@"label2.text", @"Label2 in MyViewController.m")];
[self.label3 setText:NSLocalizedString(@"label3.text", @"Label3 in MyViewController.m")];

  Continue reading iPhone – Localization