Don’t you just hate when a small change in the innards of your view controller forces you to change its header file just to conform to a delegate protocol? For example, adding emailing functionality requires you to implement the MFMailComposeViewControllerDelegate protocol and @import <MessageUI/MessageUI.h>. Talk about breaking encapsulation…
Thankfully, you can do that in your .m implementation file instead. (Even though Apple sample code doesn’t.) All you need to do is use the empty category:
@interface MyViewController ()
<MFMailComposeViewControllerDelegate> // privately conform to protocol
@property (nonatomic, strong) UIView *somePrivateSubview;
// synthesize and methods implementations
The empty category MyViewController () allows you to define private ivars, properties, methods, and even protocols – all in the privacy of your .m file, transparent to your clients.
I know, I know – not another badge class! But the thing is, the other badges laying around the net all seem overly complicated and too inflexible. So yes, I wrote another badge class. Luckily, it was really easy because I used the built-in capabilities of iOS, and it turned out very flexible and powerful, because, well, I used the built-in capabilities of iOS. No manual CoreGraphics drawing code, just automatic CALayer-s magic.
The basic badge is a UILabel whose underlying CALayer has a backgroundColor and cornerRadius:
#import <QuartzCore/QuartzCore.h> // don't forget!
UILabel *badge = [[UILabel alloc] init];
badge.layer.backgroundColor = [UIColor blueColor].CGColor;
badge.layer.cornerRadius = badge.bounds.size.height / 2;
Basically, that’s it. There are some adjustments needed to make sure the label leaves enough space around the text for the rounded corners, but that’s easily done with short overrides of textRectForBounds and drawTextInRect.
The nice thing is how easy it is to make app-icon style badges, with border, shadow and gloss:
- For border simply set the layer’s borderColor and borderWidth.
- For shadow set the layer’s shadowOpacity, shadowColor and shadowOffset.
- For gloss add a CAGradientLayer sublayer.
It’s that simple. And even better: animatable!
You can get the code, along with a Mail.app style BadgeTableViewCell and a demo app, at GitHub: http://github.com/yonat/BadgeLabel
There’s a simple way to render a UIView into a UIImage: use the view’s layer, and render it into a bitmap graphic context. Here is the code, phrased as a UIView category:
@implementation UIView (RenderUIImage)
- (UIImage *)renderAsImage
// setup context
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f); // use same scale factor as device
CGContextRef c = UIGraphicsGetCurrentContext();
// render view
// get reslting image
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
Just remember to link the QuartzCore.framework and you’re good to go.
To localize apps (which means: to make them work in other languages) you need to avoid using plain @”strings” and instead use the one of the tedious functions NSLocalizedString, NSLocalizedStringFromTable, or NSLocalizedStringFromTableInBundle, that take two to four arguments. Horror.
I mean, any sane programmer will get a headache from replacing a simple @"Great!" with this:
NSLocalizedStringFromTable(@"Great!", @"Messages", nil)
So I took a leaf from WordPress, and shortened the calls:
#define __(str) NSLocalizedStringFromTable(str, @"Messages", nil)
#define __d(str, description) NSLocalizedStringFromTable(str, @"Messages", description)
Now localization is easy and fun: I simply type
__(@"Great!") and the localization code is taken care of by the C pre-processor. Mission accomplished.