-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+KCExtension.m
388 lines (289 loc) · 10.2 KB
/
UIView+KCExtension.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//
// UIView+KCExtension.m
// categoryDemo
//
// Created by zhangweiwei on 16/5/6.
// Copyright © 2016年 Erica. All rights reserved.
//
#import "UIView+KCExtension.h"
#import "CALayer+KCExtension.h"
#import <objc/message.h>
static NSString *const KCBadgeValueLabelKey = @"kc_badgeValueLabel";
static NSString *const KCBorderLayerKey = @"kc_borderLayer";
@interface UIView ()
@property (nonatomic, strong) UILabel *kc_badgeValueLabel;
@property (nonatomic, strong) CAShapeLayer *kc_borderLayer;
@end
@implementation UIView (KCExtension)
#pragma mark -懒加载
- (CAShapeLayer *)kc_borderLayer
{
CAShapeLayer *borderLayer = objc_getAssociatedObject(self, (__bridge const void *)(KCBorderLayerKey));
if (!borderLayer) {
borderLayer = [CAShapeLayer layer];
borderLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:borderLayer];
objc_setAssociatedObject(self, (__bridge const void *)(KCBorderLayerKey), borderLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return borderLayer;
}
- (UILabel *)kc_badgeValueLabel
{
UILabel *badgeValueLabel = objc_getAssociatedObject(self, (__bridge const void *)(KCBadgeValueLabelKey));
if (!badgeValueLabel) {
badgeValueLabel = [[UILabel alloc] init];
badgeValueLabel.font = [UIFont systemFontOfSize:10];
badgeValueLabel.textColor = [UIColor whiteColor];
badgeValueLabel.textAlignment = NSTextAlignmentCenter;
badgeValueLabel.layer.backgroundColor = [UIColor redColor].CGColor;
[self addSubview:badgeValueLabel];
objc_setAssociatedObject(self, (__bridge const void *)(KCBadgeValueLabelKey), badgeValueLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return badgeValueLabel;
}
#pragma mark -frame相关
- (void)setKc_center:(CGPoint)kc_center
{
self.center = kc_center;
}
- (CGPoint)kc_center
{
return self.center;
}
- (void)setKc_x:(CGFloat)kc_x
{
CGRect frame = self.frame;
frame.origin.x = kc_x;
self.frame = frame;
}
- (CGFloat)kc_x { return self.frame.origin.x; }
- (void)setKc_maxX:(CGFloat)kc_maxX { self.kc_x = kc_maxX - self.kc_width; }
- (CGFloat)kc_maxX { return CGRectGetMaxX(self.frame); }
- (void)setKc_maxY:(CGFloat)kc_maxY { self.kc_y = kc_maxY - self.kc_height; }
- (CGFloat)kc_maxY { return CGRectGetMaxY(self.frame); }
- (void)setKc_y:(CGFloat)kc_y
{
CGRect frame = self.frame;
frame.origin.y = kc_y;
self.frame = frame;
}
- (CGFloat)kc_y { return self.frame.origin.y; }
- (void)setKc_centerX:(CGFloat)kc_centerX
{
CGPoint center = self.center;
center.x = kc_centerX;
self.center = center;
}
- (CGFloat)kc_centerX { return self.center.x; }
- (void)setKc_centerY:(CGFloat)kc_centerY
{
CGPoint center = self.center;
center.y = kc_centerY;
self.center = center;
}
- (CGFloat)kc_centerY { return self.center.y; }
- (void)setKc_width:(CGFloat)kc_width
{
CGRect frame = self.frame;
frame.size.width = kc_width;
self.frame = frame;
}
- (CGFloat)kc_width { return self.frame.size.width; }
- (void)setKc_height:(CGFloat)kc_height
{
CGRect frame = self.frame;
frame.size.height = kc_height;
self.frame = frame;
}
- (CGFloat)kc_height { return self.frame.size.height; }
- (void)setKc_size:(CGSize)kc_size
{
CGRect frame = self.frame;
frame.size = kc_size;
self.frame = frame;
}
- (CGSize)kc_size { return self.frame.size; }
/**********************/
#pragma mark -transform相关
// helper to get pre transform frame
-(CGRect)kc_originalFrameAfterTransform {
CGAffineTransform currentTransform = self.transform;
self.transform = CGAffineTransformIdentity;
CGRect originalFrame = self.frame;
self.transform = currentTransform;
return originalFrame;
}
// now get your corners
- (CGPoint)kc_topLeftAfterTransform {
CGRect frame = [self kc_originalFrameAfterTransform];
return [self kc_pointInViewAfterTransform:frame.origin];
}
- (CGPoint)kc_topRightAfterTransform {
CGRect frame = [self kc_originalFrameAfterTransform];
CGPoint point = frame.origin;
point.x += frame.size.width;
return [self kc_pointInViewAfterTransform:point];
}
- (CGPoint)kc_bottomLeftAfterTransform {
CGRect frame = [self kc_originalFrameAfterTransform];
CGPoint point = frame.origin;
point.y += frame.size.height;
return [self kc_pointInViewAfterTransform:point];
}
- (CGPoint)kc_bottomRightAfterTransform {
CGRect frame = [self kc_originalFrameAfterTransform];
CGPoint point = frame.origin;
point.x += frame.size.width;
point.y += frame.size.height;
return [self kc_pointInViewAfterTransform:point];
}
// 辅助方法
- (CGPoint)kc_pointInViewAfterTransform:(CGPoint)thePoint {
// get offset from center
CGPoint offset = CGPointMake(thePoint.x - self.center.x, thePoint.y - self.center.y);
// get transformed point
CGPoint transformedPoint = CGPointApplyAffineTransform(offset, self.transform);
// make relative to center
return CGPointMake(transformedPoint.x + self.center.x, transformedPoint.y + self.center.y);
}
/**********************/
#pragma mark -截图相关
- (UIImage *)kc_screenshot { return [self kc_screenshotWithRect:self.bounds]; }
- (UIImage *)kc_screenshotWithRect:(CGRect)rect;
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
if (context == NULL)
{
return nil;
}
CGContextSaveGState(context);
CGContextTranslateCTM(context, -rect.origin.x, -rect.origin.y);
if( [self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
{
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
}
else
{
[self.layer renderInContext:context];
}
CGContextRestoreGState(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (BOOL)kc_isDisplayOnScreen
{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
CGRect newF = [window convertRect:self.frame fromView:self.superview];
return !self.isHidden && self.alpha > 0.01 && self.window == window && CGRectIntersectsRect(newF, window.bounds);
}
/**********************/
#pragma mark -分割线相关
+ (instancetype)kc_whiteSeparator
{
UIView *view = [self new];
view.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1];
return view;
}
+ (instancetype)kc_blackSeparator
{
UIView *view = [self new];
view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
return view;
}
/**********************/
#pragma mark -xib相关
+ (instancetype)kc_viewFromXib { return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject]; }
+ (UINib *)kc_xib { return [UINib nibWithNibName:NSStringFromClass(self) bundle:nil]; }
/**********************/
#pragma mark -显示红点
- (void)setKc_badgeValue:(NSString *)kc_badgeValue
{
if (kc_badgeValue) {
self.kc_badgeValueLabel.hidden = NO;
self.kc_badgeValueLabel.text = kc_badgeValue;
if (kc_badgeValue.length) {
CGFloat h = 15;
CGFloat minW = h;
CGFloat w = [kc_badgeValue sizeWithAttributes:@{NSFontAttributeName : self.kc_badgeValueLabel.font}].width + 8;
w = w < minW ? minW : w;
self.kc_badgeValueLabel.frame = CGRectMake(self.frame.size.width - w * 0.5, - h * 0.5, w, h);
self.kc_badgeValueLabel.layer.cornerRadius = h * 0.5;
}else {
CGFloat wh = 8;
self.kc_badgeValueLabel.frame = CGRectMake(self.frame.size.width - wh * 0.5, - wh * 0.5, wh, wh);
self.kc_badgeValueLabel.layer.cornerRadius = wh * 0.5;
}
}else {
self.kc_badgeValueLabel.hidden = YES;
}
}
- (NSString *)kc_badgeValue
{
return self.kc_badgeValueLabel.text;
}
- (void)kc_setBadgeValue:(NSString *)badgeValue offset:(CGPoint)offset
{
self.kc_badgeValue = badgeValue;
CGRect temp = self.kc_badgeValueLabel.frame;
temp.origin.x = self.frame.size.width - temp.size.width * 0.5 + offset.x;
temp.origin.y = - temp.size.height * 0.5 + offset.y;
self.kc_badgeValueLabel.frame = temp;
}
/**********************/
#pragma mark -layer
- (void)setKc_layerBackgroundColor:(UIColor *)kc_layerBackgroundColor
{
self.layer.backgroundColor = kc_layerBackgroundColor.CGColor;
}
- (UIColor *)kc_layerBackgroundColor
{
return [UIColor colorWithCGColor:self.layer.backgroundColor];
}
- (void)setKc_layerBorderColor:(UIColor *)kc_layerBorderColor
{
self.layer.borderColor = kc_layerBorderColor.CGColor;
}
- (UIColor *)kc_layerBorderColor
{
return [UIColor colorWithCGColor:self.layer.borderColor];
}
- (void)setKc_layerCornerRadius:(CGFloat)kc_layerCornerRadius
{
self.layer.cornerRadius = kc_layerCornerRadius;
}
- (CGFloat)kc_layerCornerRadius
{
return self.layer.cornerRadius;
}
- (void)setKc_layerBorderWidth:(CGFloat)kc_layerBorderWidth
{
self.layer.borderWidth = kc_layerBorderWidth;
}
- (CGFloat)kc_layerBorderWidth
{
return self.layer.borderWidth;
}
- (void)kc_setLayerCornerRadiusWithClips:(CGFloat)cornerRadius
{
self.kc_layerCornerRadius = cornerRadius;
self.clipsToBounds = YES;
}
- (void)kc_setBorderWithWidth:(CGFloat)width cornerRadius:(CGFloat)radius color:(UIColor *)color
{
[self kc_setBorderWithWidth:width cornerRadius:radius color:color roundingCorners:UIRectCornerAllCorners];
}
- (void)kc_setBorderWithWidth:(CGFloat)width cornerRadius:(CGFloat)radius color:(UIColor *)color roundingCorners:(UIRectCorner)corners
{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, width, width) byRoundingCorners:corners cornerRadii:CGSizeMake(radius-width, radius-width)];
self.kc_borderLayer.path = path.CGPath;
self.kc_borderLayer.lineWidth = width;
self.kc_borderLayer.strokeColor = color.CGColor;
self.kc_borderLayer.frame = self.bounds;
}
- (void)kc_setRoundedCoverWithBackgroundColor:(UIColor *)color cornerRadius:(CGFloat)radius
{
[self.layer kc_setRoundedCoverWithBackgroundColor:color.CGColor cornerRadius:radius];
}
@end