Created
April 24, 2015 17:05
-
-
Save zoejessica/7f4ff2e4c41675f376fc to your computer and use it in GitHub Desktop.
Badged pin (eg for maps) using IB_Designable and IBInspectable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
IB_DESIGNABLE @interface BadgedPin : UIView | |
@property (nonatomic, assign) IBInspectable CGFloat badgeDiameter; | |
@property (nonatomic, strong) IBInspectable UIColor *badgeColor; | |
@property (nonatomic, assign) IBInspectable CGFloat badgeStrokeWidth; | |
@property (nonatomic, strong) IBInspectable UIColor *fillColor; | |
@property (nonatomic, strong) IBInspectable NSString *imageName; | |
@end | |
#import "BadgedPin.h" | |
@interface BadgedPin() | |
@property (strong, nonatomic) UIImage *badge; | |
@property (strong, nonatomic) UIImage *underlyingImage; | |
@end | |
@implementation BadgedPin | |
- (void)drawRect:(CGRect)rect { | |
CGRect bounds = self.bounds; | |
CGFloat xposition = CGRectGetMaxX(bounds)/2 - self.badgeDiameter/2; | |
CGFloat yposition = CGRectGetMaxY(bounds)/2 - self.badgeDiameter/2; | |
CGPoint topLeftOfCentre = CGPointMake(xposition, yposition); | |
CGRect rectangle = CGRectMake(topLeftOfCentre.x, topLeftOfCentre.y, self.badgeDiameter, self.badgeDiameter); | |
UIBezierPath *outerCircle = [UIBezierPath bezierPathWithOvalInRect:rectangle]; | |
[self.badgeColor setStroke]; | |
[outerCircle setLineWidth:self.badgeStrokeWidth]; | |
[self.fillColor setFill]; | |
[outerCircle stroke]; | |
[outerCircle fill]; | |
[self.badge drawInRect:CGRectInset(rectangle, 4, 4)]; | |
} | |
- (id)initWithFrame:(CGRect)frame { | |
self = [super initWithFrame:frame]; | |
if (self) { | |
self.opaque = NO; | |
} | |
return self; | |
} | |
- (void)setImageName:(NSString *)imageName { | |
_imageName = imageName; | |
self.underlyingImage = [UIImage imageNamed:imageName]; | |
[self updateBadgeWithImage:self.underlyingImage]; | |
} | |
- (void)prepareForInterfaceBuilder { | |
NSBundle *currentBundle = [NSBundle bundleForClass:[self class]]; | |
self.underlyingImage = [UIImage imageNamed:self.imageName inBundle:currentBundle compatibleWithTraitCollection:self.traitCollection]; | |
[self updateBadgeWithImage:self.underlyingImage]; | |
} | |
- (void)updateBadgeWithImage:(UIImage *)image { | |
self.badge = [self tintImage:image withColor:self.badgeColor]; | |
} | |
- (void)setBadgeColor:(UIColor *)badgeColor { | |
_badgeColor = badgeColor; | |
[self updateBadgeWithImage:self.underlyingImage]; | |
} | |
- (UIImage *)tintImage:(UIImage *)image withColor:(UIColor *)color { | |
// http://stackoverflow.com/questions/3514066/how-to-tint-a-transparent-png-image-in-iphone | |
UIGraphicsBeginImageContextWithOptions (image.size, NO, [[UIScreen mainScreen] scale]); // for correct resolution on retina, thanks @MobileVet | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(context, 0, image.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); | |
// image drawing code here | |
// draw tint color | |
CGContextSetBlendMode(context, kCGBlendModeNormal); | |
[color setFill]; | |
CGContextFillRect(context, rect); | |
// mask by alpha values of original image | |
CGContextSetBlendMode(context, kCGBlendModeDestinationIn); | |
CGContextDrawImage(context, rect, image.CGImage); | |
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return coloredImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment