Compare method
Either you implement a compare-method for your object:
- (NSComparisonResult)compare:(Person *)otherObject {
return [self.birthDate compare:otherObject.birthDate];
}
NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];
NSSortDescriptor (better)
or usually even better:
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate"
ascending:YES];
NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDescriptor]];
You can easily sort by multiple keys by adding more than one to the array. Using custom comparator-methods is possible as well. Have a look at the documentation.
Blocks (shiny!)
There's also the possibility of sorting with a block since Mac OS X 10.6 and iOS 4:
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(Person *a, Person *b) {
return [a.birthDate compare:b.birthDate];
}];
Performance
The -compare:
and block-based methods will be quite a bit faster, in general, than using NSSortDescriptor
as the latter relies on KVC. The primary advantage of the NSSortDescriptor
method is that it provides a way to define your sort order using data, rather than code, which makes it easy to e.g. set things up so users can sort an NSTableView
by clicking on the header row.
UPDATE: Xcode 6 Interface Builder now allows you to select custom fonts and will render them at design time correctly.
I know this question is quite old, but I've been struggling to find an easy way to specify a custom font in Storyboard (or Interface Builder) easily for iOS 5 and I found a quite convenient solution.
First, make sure that you've added the font to the project by following this tutorial. Also remember that UINavigationBar, UITabBar and UISegmentedControl custom font can be specified by using the setTitleTextAttributes:
method of the UIAppearance proxy.
Add the categories below to the project for UIButton, UITextField, UILabel and any other component that needs a custom font. The categories simply implement a new property fontName
which changes the current font of the element while maintaining the font size.
To specify the font in Storyboard, just select the desired element (label, button, textview, etc) and add a User Defined Runtime Attribute with Key Path set to fontName of type String and value with the name of your custom font.
And that's it, you don't even need to import the categories. This way you don't need an outlet for every UI component that requires a custom font and you don't need to code it manually.
Take into account that the font won't show in Storyboard, but you will see it when running on your device or simulator.
Category files
UIButton+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UIButton (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UIButton+TCCustomFont.m:
#import "UIButton+TCCustomFont.h"
@implementation UIButton (TCCustomFont)
- (NSString *)fontName {
return self.titleLabel.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.titleLabel.font = [UIFont fontWithName:fontName size:self.titleLabel.font.pointSize];
}
@end
UILabel+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UILabel (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UILabel+TCCustomFont.m:
#import "UILabel+TCCustomFont.h"
@implementation UILabel (TCCustomFont)
- (NSString *)fontName {
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
@end
UITextField+TCCustomFont.h:
#import <UIKit/UIKit.h>
@interface UITextField (TCCustomFont)
@property (nonatomic, copy) NSString* fontName;
@end
UITextField+TCCustomFont.m:
#import "UITextField+TCCustomFont.h"
@implementation UITextField (TCCustomFont)
- (NSString *)fontName {
return self.font.fontName;
}
- (void)setFontName:(NSString *)fontName {
self.font = [UIFont fontWithName:fontName size:self.font.pointSize];
}
@end
Also downloadable from GIST and also as a single file.
Troubleshooting
If you run against runtime errors because the fontName
property is not specified just add the flag -all_load
under Other linker Flags in project settings to force the linker to include the categories.
Best Answer
Although you can't specify both a custom font and a preferred text style via Storyboard, it's not difficult to programmatically specify a dynamic type size for your custom font:
Swift:
When you receive a
UIContentSizeCategoryDidChangeNotification
, use the same code to update your label's font.Obj C: