Created
October 10, 2015 06:23
-
-
Save eoghain/92d55617a3a15b6a2807 to your computer and use it in GitHub Desktop.
UIView that can be used as a map marker on GoogleMaps. marker.icon = [[[MapClusterPinView alloc] initWithCount:12] image];
This file contains hidden or 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
// | |
// MapClusterPinView.h | |
// | |
// Created by Rob Booth on 10/9/15. | |
// Copyright © 2015 Rob Booth. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface MapClusterPinView : UIView | |
- (instancetype _Nullable)initWithCount:(NSInteger)count; | |
- (UIImage * _Nonnull)image; | |
@end |
This file contains hidden or 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
// | |
// MapClusterPinView.m | |
// | |
// Created by Rob Booth on 10/9/15. | |
// Copyright © 2015 Rob Booth. All rights reserved. | |
// | |
#import "MapClusterPinView.h" | |
@interface MapClusterPinView () | |
@property (assign) CGSize size; | |
@property (strong, nonatomic) UIColor *circleColor; | |
@property (strong, nonatomic) NSString *count; | |
@end | |
@implementation MapClusterPinView | |
- (instancetype)initWithCount:(NSInteger)count | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
self.size = CGSizeMake(30, 30); | |
if (count < 10) | |
{ | |
self.size = CGSizeMake(15,15); | |
} | |
else if (count < 101) | |
{ | |
self.size = CGSizeMake(20,20); | |
} | |
self.circleColor = [self randomColor]; | |
self.count = [NSString stringWithFormat:@"%ld%@", (count < 100) ? count : 100, (count > 100) ? @"+" : @""]; | |
} | |
return self; | |
} | |
- (UIColor *)randomColor | |
{ | |
// colors from http://designmodo.github.io/Flat-UI/ | |
NSArray *colors = @[ | |
[UIColor colorWithRed:0.10196078431372549 green:0.7372549019607844 blue:0.611764705882353 alpha:1.0], | |
[UIColor colorWithRed:0.10196078431372549 green:0.7372549019607844 blue:0.611764705882353 alpha:1.0], | |
[UIColor colorWithRed:0.08627450980392157 green:0.6274509803921569 blue:0.5215686274509804 alpha:1.0], | |
[UIColor colorWithRed:0.1803921568627451 green:0.8 blue:0.44313725490196076 alpha:1.0], | |
[UIColor colorWithRed:0.15294117647058825 green:0.6823529411764706 blue:0.3764705882352941 alpha:1.0], | |
[UIColor colorWithRed:0.20392156862745098 green:0.596078431372549 blue:0.8588235294117647 alpha:1.0], | |
[UIColor colorWithRed:0.1607843137254902 green:0.5019607843137255 blue:0.7254901960784313 alpha:1.0], | |
[UIColor colorWithRed:0.6078431372549019 green:0.34901960784313724 blue:0.7137254901960784 alpha:1.0], | |
[UIColor colorWithRed:0.5568627450980392 green:0.26666666666666666 blue:0.6784313725490196 alpha:1.0], | |
[UIColor colorWithRed:0.20392156862745098 green:0.28627450980392155 blue:0.3686274509803922 alpha:1.0], | |
[UIColor colorWithRed:0.17254901960784313 green:0.24313725490196078 blue:0.3137254901960784 alpha:1.0], | |
[UIColor colorWithRed:0.9450980392156862 green:0.7686274509803922 blue:0.058823529411764705 alpha:1.0], | |
[UIColor colorWithRed:0.9529411764705882 green:0.611764705882353 blue:0.07058823529411765 alpha:1.0], | |
[UIColor colorWithRed:0.9019607843137255 green:0.49411764705882355 blue:0.13333333333333333 alpha:1.0], | |
[UIColor colorWithRed:0.8274509803921568 green:0.32941176470588235 blue:0 alpha:1.0], | |
[UIColor colorWithRed:0.9058823529411765 green:0.2980392156862745 blue:0.23529411764705882 alpha:1.0], | |
[UIColor colorWithRed:0.7529411764705882 green:0.2235294117647059 blue:0.16862745098039217 alpha:1.0], | |
[UIColor colorWithRed:0.9254901960784314 green:0.9411764705882353 blue:0.9450980392156862 alpha:1.0], | |
[UIColor colorWithRed:0.7411764705882353 green:0.7647058823529411 blue:0.7803921568627451 alpha:1.0], | |
[UIColor colorWithRed:0.5843137254901961 green:0.6470588235294118 blue:0.6509803921568628 alpha:1.0], | |
[UIColor colorWithRed:0.4980392156862745 green:0.5490196078431373 blue:0.5529411764705883 alpha:1.0] | |
]; | |
return colors[arc4random() % colors.count]; | |
} | |
- (void)drawRect:(CGRect)rect | |
{ | |
//// General Declarations | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
//// Circle Drawing | |
UIBezierPath* circlePath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(0, 0, self.size.width, self.size.height)]; | |
[self.circleColor setFill]; | |
[circlePath fill]; | |
[UIColor.whiteColor setStroke]; | |
circlePath.lineWidth = 1; | |
[circlePath stroke]; | |
//// Text Drawing | |
CGRect textRect = CGRectMake(0, self.size.height/2 - 5, self.size.width, 10); | |
NSMutableParagraphStyle* textStyle = NSMutableParagraphStyle.defaultParagraphStyle.mutableCopy; | |
textStyle.alignment = NSTextAlignmentCenter; | |
NSDictionary* textFontAttributes = @{NSFontAttributeName: [UIFont fontWithName: @"Avenir-Heavy" size: 10], NSForegroundColorAttributeName: UIColor.whiteColor, NSParagraphStyleAttributeName: textStyle}; | |
CGFloat textTextHeight = [self.count boundingRectWithSize: CGSizeMake(textRect.size.width, INFINITY) options: NSStringDrawingUsesLineFragmentOrigin attributes: textFontAttributes context: nil].size.height; | |
CGContextSaveGState(context); | |
CGContextClipToRect(context, textRect); | |
[self.count drawInRect: CGRectMake(CGRectGetMinX(textRect), CGRectGetMinY(textRect) + (CGRectGetHeight(textRect) - textTextHeight) / 2, CGRectGetWidth(textRect), textTextHeight) withAttributes: textFontAttributes]; | |
CGContextRestoreGState(context); | |
} | |
- (UIImage *)image | |
{ | |
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) { | |
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]); | |
} else { | |
UIGraphicsBeginImageContext(self.size); | |
} | |
[self drawRect:CGRectMake(0, 0, self.size.width, self.size.height)]; | |
UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return img; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment