Quick Tip
Rounding the corners of an UIView
1Rounded corners are all the rage at the moment, so you would think that there would be an API on UIView to do such a thing. If you looked at all of the UIView documentation, you would come to the conclusion that there is not an API to do such a thing, and you would be (kind of) wrong.
Although UIView does not allow you round its corners, CALayer does, and every UIView is backed by a CALayer.
To access all of the functionality of CALayer, you will need to import QuartzCore into any of header/implementation files that you intend to use CALayer in.
#import <QuartzCore/QuartzCore.h>CALayer has a lot of APIs available to it, but the property we are interested in is:
@property CGFloat cornerRadiusAs the property suggests, this allows you to set the CALayer‘s corner radius in points:
UIView *view = [[UIView alloc] initWithFrame:frame]; view.layer.cornerRadius = 10.0;
CALayer also has properties allowing you to set its border, shadow etc, so all of these can save you a lot of code … and unwanted images.
Show hidden files in the Finder
4Sometimes when you are digging around in the under pinnings of Mac OS X, you need to access folders and files that are normally hidden by the OS. Thankfully there is preference in the Finder for this, which you can turn on and off in the Terminal.
The command is:
defaults write com.apple.Finder AppleShowAllFiles YES
The Finder needs to be restarted for this change to take effect. Luckily there is a Terminal command for this too:
killall Finder
Once you are done playing around with hidden files, you can stop showing them by replacing YES with NO in the original terminal command.
defaults write com.apple.Finder AppleShowAllFiles NO
It is as simple as that, but make sure your don’t break anything.
Reopening an application’s main window by clicking on the Dock Icon
1When building Mac applications, Apple usually takes care of most of the default behaviours for you. One thing that Mac applications don’t do by default is, reopening the applications main window (if it has been closed), when the dock icon is pressed.
Although this information is very hard to find in the documentation, but is actually very easy to. The method you need to find is:
1 | - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag; |
This is an optional delegate method that your AppDelegate can choose to implement, and is called when the user presses your application’s dock icon. The bool flags indicates whether the application has any visible windows. To reopen your application’s main window, you need to have a pointer to it (In the example below assume that it is defined as NSWindow *window; in the header file). If you do have a pointer to then you simple need to implement the code below.
1 2 3 4 5 6 7 8 | - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag{ if(flag==NO){ [window makeKeyAndOrderFront:self]; } return YES; } |
It is as simple as that.
Changing the name of an iPhone application
1When you finally come to release your application after months of hard work, your might want to change the name of the application that appears on the iPhone springboard, and if you use it, the settings application.
To change the name of an iPhone application is very easy. Simply open the Xcode project, and then scroll down to the “Targets” section, which the the “Groups & Files” part of Xcode. Select the application’s target and “Get info” on it using cmd-i or ctrl clicking it, and select “Get Info” from the menu.
In the build section of the get info window, you need to change the “Product Name” value to whatever you want your application to be called. When you change this value make sure that the configuration pop up menu (at the top of the window) is set to “All Configurations”, so it takes effect in all of your builds.
For the change of name to take effect, simply clean your targets and rebuild your application.
Make your iPhone vibrate
0One thing that took me a while to find out, was how to make an iPhone vibrate. In the end I found out that it is in the audio APIs, and it is just the one line of code.
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Categories
4Ever thought “Oh I wish Apple had included method X for class Y”, well that is what categories are for.
Categories are normally used for 2 things.
- Extending a class (using methods, not variables !!!)
- Logically separating a class into different functional areas
So lets look at something that is extremely popular at the moment ….. Twitter, and in paticular the 140 charachter limit.
Apple (for obvious reasons) don’t include Twitter related method calls on there string class NSString.
At the moment you would have to write everywhere in you code, something the resembles the following
NSString *twitterString=@"Objective-C rocks, I never want to look at Java again"; if([[twitterString] length]< 141) { //Post to twitter }
Now this is good, but what if twitter changed their character limit, you would have to go an edit your code everywhere, where you used this check.
So we are going add a method to the NSString class:
- (BOOL)isUnderTwitterCharachterLimit;
This will make are code look like this
NSString *twitterString=@"Objective-C rocks, I never want to look at Java again"; if([twitterString isUnderTwitterCharachterLimit]) { //Post to twitter }
You now need an interface and implementation file, Im going to call them NSStringTwitterCategory.h and NSStringTwitterCategory.m
The interface file (NSStringTwitterCategory.h) is extremely small:
#import <Foundation/Foundation.h> @interface NSString (Twitter) -(BOOL)isUnderTwitterCharachterLimit; @end
Instead of defining a new class in the format:
@interface NewClass: SuperClass{
}
You extend an existing class using the format:
@interface ClassYourExtending (CategoryName)
You can call the category whatever you want, but obviously you want to make it clear.
You then declare the methods like you always do.
The implementation file (NSStringTwitterCategory.m) is also not very complex:
#import "NSStringTwitterCategory.h"
@implementation NSString (Twitter)
-(BOOL)isUnderTwitterCharachterLimit{
if ( [ [self length] <141])
return YES;
else
return NO;
}
@end
Again you extend the class in the same way as you did in the header file:
@implementation NSString (Twitter)
And you simple define the method as you normally would, nothing strange or fancy. If you want to use this category you just need to import the header file where you want to use it.
This is obviously a very simple example, but I hope it shows you how to use categories in Objective-C.
Does a NSString contain a substring ?
15Here is a little tip on how to tell if a string contains another string, using the underused data type NSRange.
NSRange gives the starting location and the length of a given value, and is often used with arrays and strings. On this occasion we will use it to find the range of a substring within another string. If the range has a location, it contains the given substring. The following code does just that.
1 2 3 4 5 6 7 8 9 | NSRange textRange; textRange =[string rangeOfString:substring]; if(textRange.location != NSNotFound) { //Does contain the substring } |
Making this a case insensitive compare is also very trivial, and can be done by lowercasing both strings
1 2 3 4 5 6 7 8 9 10 | NSRange textRange; textRange =[[string lowercaseString] rangeOfString:[substring lowercaseString]]; if(textRange.location != NSNotFound) { //Does contain the substring } |