-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImage+FlatUI.m
executable file
·163 lines (142 loc) · 6.68 KB
/
UIImage+FlatUI.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// UIImage+Color.m
// FlatUI
//
// Created by Jack Flintermann on 5/3/13.
// Copyright (c) 2013 Jack Flintermann. All rights reserved.
//
#import "UIImage+FlatUI.h"
@implementation UIImage (FlatUI)
static CGFloat edgeSizeFromCornerRadius(CGFloat cornerRadius) {
return cornerRadius * 2 + 1;
}
+ (UIImage *)imageWithColor:(UIColor *)color
cornerRadius:(CGFloat)cornerRadius {
CGFloat minEdgeSize = edgeSizeFromCornerRadius(cornerRadius);
CGRect rect = CGRectMake(0, 0, minEdgeSize, minEdgeSize);
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
roundedRect.lineWidth = 0;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
[color setFill];
[roundedRect fill];
[roundedRect stroke];
[roundedRect addClip];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(cornerRadius, cornerRadius, cornerRadius, cornerRadius)];
}
+ (UIImage *) buttonImageWithColor:(UIColor *)color
cornerRadius:(CGFloat)cornerRadius
shadowColor:(UIColor *)shadowColor
shadowInsets:(UIEdgeInsets)shadowInsets {
UIImage *topImage = [self imageWithColor:color cornerRadius:cornerRadius];
UIImage *bottomImage = [self imageWithColor:shadowColor cornerRadius:cornerRadius];
CGFloat totalHeight = edgeSizeFromCornerRadius(cornerRadius) + shadowInsets.top + shadowInsets.bottom;
CGFloat totalWidth = edgeSizeFromCornerRadius(cornerRadius) + shadowInsets.left + shadowInsets.right;
CGFloat topWidth = edgeSizeFromCornerRadius(cornerRadius);
CGFloat topHeight = edgeSizeFromCornerRadius(cornerRadius);
CGRect topRect = CGRectMake(shadowInsets.left, shadowInsets.top, topWidth, topHeight);
CGRect bottomRect = CGRectMake(0, 0, totalWidth, totalHeight);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(totalWidth, totalHeight), NO, 0.0f);
if (!CGRectEqualToRect(bottomRect, topRect)) {
[bottomImage drawInRect:bottomRect];
}
[topImage drawInRect:topRect];
UIImage *buttonImage = UIGraphicsGetImageFromCurrentImageContext();
UIEdgeInsets resizeableInsets = UIEdgeInsetsMake(cornerRadius + shadowInsets.top,
cornerRadius + shadowInsets.left,
cornerRadius + shadowInsets.bottom,
cornerRadius + shadowInsets.right);
UIGraphicsEndImageContext();
return [buttonImage resizableImageWithCapInsets:resizeableInsets];
}
+ (UIImage *) circularImageWithColor:(UIColor *)color
size:(CGSize)size {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:rect];
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0f);
[color setFill];
[color setStroke];
[circle addClip];
[circle fill];
[circle stroke];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *) imageWithMinimumSize:(CGSize)size {
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width, size.height), NO, 0.0f);
[self drawInRect:rect];
UIImage *resized = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [resized resizableImageWithCapInsets:UIEdgeInsetsMake(size.height/2, size.width/2, size.height/2, size.width/2)];
}
+ (UIImage *) stepperPlusImageWithColor:(UIColor *)color {
CGFloat iconEdgeSize = 15;
CGFloat iconInternalEdgeSize = 3;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(iconEdgeSize, iconEdgeSize), NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[color setFill];
CGFloat padding = (iconEdgeSize - iconInternalEdgeSize) / 2;
CGContextFillRect(context, CGRectMake(padding, 0, iconInternalEdgeSize, iconEdgeSize));
CGContextFillRect(context, CGRectMake(0, padding, iconEdgeSize, iconInternalEdgeSize));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *) stepperMinusImageWithColor:(UIColor *)color {
CGFloat iconEdgeSize = 15;
CGFloat iconInternalEdgeSize = 3;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(iconEdgeSize, iconEdgeSize), NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[color setFill];
CGFloat padding = (iconEdgeSize - iconInternalEdgeSize) / 2;
CGContextFillRect(context, CGRectMake(0, padding, iconEdgeSize, iconInternalEdgeSize));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *) backButtonImageWithColor:(UIColor *)color
barMetrics:(UIBarMetrics) metrics
cornerRadius:(CGFloat)cornerRadius {
CGSize size;
if (metrics == UIBarMetricsDefault) {
size = CGSizeMake(50, 30);
} else {
size = CGSizeMake(60, 23);
}
UIBezierPath *path = [self bezierPathForBackButtonInRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:cornerRadius];
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
[color setFill];
[path addClip];
[path fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(cornerRadius, 15, cornerRadius, cornerRadius)];
}
+ (UIBezierPath *) bezierPathForBackButtonInRect:(CGRect)rect cornerRadius:(CGFloat)radius {
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint mPoint = CGPointMake(CGRectGetMaxX(rect) - radius, rect.origin.y);
CGPoint ctrlPoint = mPoint;
[path moveToPoint:mPoint];
ctrlPoint.y += radius;
mPoint.x += radius;
mPoint.y += radius;
if (radius > 0) [path addArcWithCenter:ctrlPoint radius:radius startAngle:(float)M_PI + (float)M_PI_2 endAngle:0 clockwise:YES];
mPoint.y = CGRectGetMaxY(rect) - radius;
[path addLineToPoint:mPoint];
ctrlPoint = mPoint;
mPoint.y += radius;
mPoint.x -= radius;
ctrlPoint.x -= radius;
if (radius > 0) [path addArcWithCenter:ctrlPoint radius:radius startAngle:0 endAngle:(float)M_PI_2 clockwise:YES];
mPoint.x = rect.origin.x + (10.0f);
[path addLineToPoint:mPoint];
[path addLineToPoint:CGPointMake(rect.origin.x, CGRectGetMidY(rect))];
mPoint.y = rect.origin.y;
[path addLineToPoint:mPoint];
[path closePath];
return path;
}
@end