@@ -28,48 +28,90 @@ of this software and associated documentation files (the "Software"), to deal
28
28
#error JSBadgeView must be compiled with ARC.
29
29
#endif
30
30
31
- #define kDefaultBadgeTextColor [UIColor whiteColor ]
32
- #define kDefaultBadgeBackgroundColor [UIColor redColor ]
33
- #define kDefaultOverlayColor [UIColor colorWithWhite: 1 .0f alpha: 0.3 ]
31
+ static const CGFloat JSBadgeViewShadowRadius = 1 .0f ;
32
+ static const CGFloat JSBadgeViewHeight = 16 .0f ;
33
+ static const CGFloat JSBadgeViewTextSideMargin = 8 .0f ;
34
+ static const CGFloat JSBadgeViewCornerRadius = 10 .0f ;
34
35
35
- #define kDefaultBadgeTextFont [UIFont boldSystemFontOfSize: [UIFont systemFontSize ]]
36
-
37
- #define kDefaultBadgeShadowColor [UIColor clearColor ]
38
-
39
- #define kDefaultBadgeStrokeColor [UIColor whiteColor ]
40
- #define kBadgeStrokeWidth 2 .0f
36
+ // Thanks to Peter Steinberger: https://gist.github.com/steipete/6526860
37
+ static BOOL JSBadgeViewIsUIKitFlatMode (void )
38
+ {
39
+ static BOOL isUIKitFlatMode = NO ;
40
+ static dispatch_once_t onceToken;
41
+ dispatch_once (&onceToken, ^{
42
+ #ifndef kCFCoreFoundationVersionNumber_iOS_7_0
43
+ #define kCFCoreFoundationVersionNumber_iOS_7_0 847.2
44
+ #endif
41
45
42
- #define kMarginToDrawInside (kBadgeStrokeWidth * 2 )
46
+ if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iOS_7_0 )
47
+ {
48
+ // If your app is running in legacy mode, tintColor will be nil - else it must be set to some color.
49
+ if (UIApplication.sharedApplication .keyWindow )
50
+ {
51
+ isUIKitFlatMode = [UIApplication.sharedApplication.keyWindow performSelector: @selector (tintColor )] != nil ;
52
+ }
53
+ else
54
+ {
55
+ // Possible that we're called early on (e.g. when used in a Storyboard). Adapt and use a temporary window.
56
+ isUIKitFlatMode = [[[UIWindow alloc ] init ] performSelector: @selector (tintColor )] != nil ;
57
+ }
58
+ }
59
+ });
43
60
44
- #define kShadowOffset CGSizeMake (0 .0f , 3 .0f )
45
- #define kShadowOpacity 0 .4f
46
- #define kDefaultShadowColor [UIColor colorWithWhite: 0 .0f alpha: kShadowOpacity ]
47
- #define kShadowRadius 1 .0f
61
+ return isUIKitFlatMode;
62
+ }
48
63
49
- #define kBadgeHeight 16 .0f
50
- #define kBadgeTextSideMargin 8 .0f
64
+ @implementation JSBadgeView
51
65
52
- #define kBadgeCornerRadius 10 .0f
66
+ + (void )applyCommonStyle
67
+ {
68
+ JSBadgeView *badgeViewAppearanceProxy = JSBadgeView.appearance ;
53
69
54
- #define kDefaultBadgeAlignment JSBadgeViewAlignmentTopRight
70
+ badgeViewAppearanceProxy.backgroundColor = UIColor.clearColor ;
71
+ badgeViewAppearanceProxy.badgeAlignment = JSBadgeViewAlignmentTopRight;
72
+ badgeViewAppearanceProxy.badgeBackgroundColor = UIColor.redColor ;
73
+ badgeViewAppearanceProxy.badgeTextFont = [UIFont boldSystemFontOfSize: UIFont.systemFontSize];
74
+ badgeViewAppearanceProxy.badgeTextColor = UIColor.whiteColor ;
75
+ }
55
76
56
- @implementation JSBadgeView
77
+ + (void )applyLegacyStyle
78
+ {
79
+ JSBadgeView *badgeViewAppearanceProxy = JSBadgeView.appearance ;
80
+
81
+ badgeViewAppearanceProxy.badgeOverlayColor = [UIColor colorWithWhite: 1 .0f alpha: 0.3 ];
82
+ badgeViewAppearanceProxy.badgeTextShadowColor = UIColor.clearColor ;
83
+ badgeViewAppearanceProxy.badgeShadowColor = [UIColor colorWithWhite: 0 .0f alpha: 0 .4f ];
84
+ badgeViewAppearanceProxy.badgeShadowSize = CGSizeMake (0 .0f , 3 .0f );
85
+ badgeViewAppearanceProxy.badgeStrokeWidth = 2 .0f ;
86
+ badgeViewAppearanceProxy.badgeStrokeColor = UIColor.whiteColor ;
87
+ }
57
88
58
- - (void )awakeFromNib
89
+ + (void )applyIOS7Style
59
90
{
60
- [ super awakeFromNib ] ;
91
+ JSBadgeView *badgeViewAppearanceProxy = JSBadgeView. appearance ;
61
92
62
- [self _init ];
93
+ badgeViewAppearanceProxy.badgeOverlayColor = UIColor.clearColor ;
94
+ badgeViewAppearanceProxy.badgeTextShadowColor = UIColor.clearColor ;
95
+ badgeViewAppearanceProxy.badgeShadowColor = UIColor.clearColor ;
96
+ badgeViewAppearanceProxy.badgeStrokeWidth = 0 .0f ;
97
+ badgeViewAppearanceProxy.badgeStrokeColor = badgeViewAppearanceProxy.badgeBackgroundColor ;
63
98
}
64
99
65
- - ( id ) initWithFrame : ( CGRect ) frame
100
+ + ( void ) initialize
66
101
{
67
- if (( self = [ super initWithFrame: frame]) )
102
+ if (self == JSBadgeView. class )
68
103
{
69
- [self _init ];
70
- }
104
+ [self applyCommonStyle ];
71
105
72
- return self;
106
+ if (JSBadgeViewIsUIKitFlatMode ())
107
+ {
108
+ [self applyIOS7Style ];
109
+ }
110
+ else
111
+ {
112
+ [self applyLegacyStyle ];
113
+ }
114
+ }
73
115
}
74
116
75
117
- (id )initWithParentView : (UIView *)parentView alignment : (JSBadgeViewAlignment)alignment
@@ -83,23 +125,13 @@ - (id)initWithParentView:(UIView *)parentView alignment:(JSBadgeViewAlignment)al
83
125
return self;
84
126
}
85
127
86
- - (void )_init
87
- {
88
- self.backgroundColor = [UIColor clearColor ];
89
-
90
- _badgeAlignment = kDefaultBadgeAlignment ;
91
-
92
- _badgeBackgroundColor = kDefaultBadgeBackgroundColor ;
93
- _badgeOverlayColor = kDefaultOverlayColor ;
94
- _badgeTextColor = kDefaultBadgeTextColor ;
95
- _badgeTextShadowColor = kDefaultBadgeShadowColor ;
96
- _badgeTextFont = kDefaultBadgeTextFont ;
97
- _badgeShadowColor = kDefaultBadgeShadowColor ;
98
- _badgeStrokeColor = kDefaultBadgeStrokeColor ;
99
- }
100
-
101
128
#pragma mark - Layout
102
129
130
+ - (CGFloat )marginToDrawInside
131
+ {
132
+ return self.badgeStrokeWidth * 2 .0f ;
133
+ }
134
+
103
135
- (void )layoutSubviews
104
136
{
105
137
[super layoutSubviews ];
@@ -108,9 +140,10 @@ - (void)layoutSubviews
108
140
const CGRect superviewBounds = CGRectIsEmpty (_frameToPositionInRelationWith) ? self.superview .bounds : _frameToPositionInRelationWith;
109
141
110
142
const CGFloat textWidth = [self sizeOfTextForCurrentSettings ].width ;
111
-
112
- const CGFloat viewWidth = textWidth + kBadgeTextSideMargin + (kMarginToDrawInside * 2 );
113
- const CGFloat viewHeight = kBadgeHeight + (kMarginToDrawInside * 2 );
143
+
144
+ const CGFloat marginToDrawInside = [self marginToDrawInside ];
145
+ const CGFloat viewWidth = textWidth + JSBadgeViewTextSideMargin + (marginToDrawInside * 2 );
146
+ const CGFloat viewHeight = JSBadgeViewHeight + (marginToDrawInside * 2 );
114
147
115
148
const CGFloat superviewWidth = superviewBounds.size .width ;
116
149
const CGFloat superviewHeight = superviewBounds.size .height ;
@@ -250,6 +283,17 @@ - (void)setBadgeBackgroundColor:(UIColor *)badgeBackgroundColor
250
283
}
251
284
}
252
285
286
+ - (void )setBadgeStrokeWidth : (CGFloat )badgeStrokeWidth
287
+ {
288
+ if (badgeStrokeWidth != _badgeStrokeWidth)
289
+ {
290
+ _badgeStrokeWidth = badgeStrokeWidth;
291
+
292
+ [self setNeedsLayout ];
293
+ [self setNeedsDisplay ];
294
+ }
295
+ }
296
+
253
297
- (void )setBadgeStrokeColor : (UIColor *)badgeStrokeColor
254
298
{
255
299
if (badgeStrokeColor != _badgeStrokeColor)
@@ -270,6 +314,16 @@ - (void)setBadgeShadowColor:(UIColor *)badgeShadowColor
270
314
}
271
315
}
272
316
317
+ - (void )setBadgeShadowSize : (CGSize )badgeShadowSize
318
+ {
319
+ if (!CGSizeEqualToSize (badgeShadowSize, _badgeShadowSize))
320
+ {
321
+ _badgeShadowSize = badgeShadowSize;
322
+
323
+ [self setNeedsDisplay ];
324
+ }
325
+ }
326
+
273
327
#pragma mark - Drawing
274
328
275
329
- (void )drawRect : (CGRect )rect
@@ -279,18 +333,19 @@ - (void)drawRect:(CGRect)rect
279
333
if (anyTextToDraw)
280
334
{
281
335
CGContextRef ctx = UIGraphicsGetCurrentContext ();
336
+
337
+ const CGFloat marginToDrawInside = [self marginToDrawInside ];
338
+ const CGRect rectToDraw = CGRectInset (rect, marginToDrawInside, marginToDrawInside);
282
339
283
- const CGRect rectToDraw = CGRectInset (rect, kMarginToDrawInside , kMarginToDrawInside );
284
-
285
- UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect: rectToDraw byRoundingCorners: (UIRectCorner)UIRectCornerAllCorners cornerRadii: CGSizeMake (kBadgeCornerRadius , kBadgeCornerRadius )];
340
+ UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect: rectToDraw byRoundingCorners: (UIRectCorner)UIRectCornerAllCorners cornerRadii: CGSizeMake (JSBadgeViewCornerRadius, JSBadgeViewCornerRadius)];
286
341
287
342
/* Background and shadow */
288
343
CGContextSaveGState (ctx);
289
344
{
290
345
CGContextAddPath (ctx, borderPath.CGPath );
291
346
292
347
CGContextSetFillColorWithColor (ctx, self.badgeBackgroundColor .CGColor );
293
- CGContextSetShadowWithColor (ctx, kShadowOffset , kShadowRadius , self.badgeShadowColor .CGColor );
348
+ CGContextSetShadowWithColor (ctx, self. badgeShadowSize , JSBadgeViewShadowRadius , self.badgeShadowColor .CGColor );
294
349
295
350
CGContextDrawPath (ctx, kCGPathFill );
296
351
}
@@ -327,7 +382,7 @@ - (void)drawRect:(CGRect)rect
327
382
{
328
383
CGContextAddPath (ctx, borderPath.CGPath );
329
384
330
- CGContextSetLineWidth (ctx, kBadgeStrokeWidth );
385
+ CGContextSetLineWidth (ctx, self. badgeStrokeWidth );
331
386
CGContextSetStrokeColorWithColor (ctx, self.badgeStrokeColor .CGColor );
332
387
333
388
CGContextDrawPath (ctx, kCGPathStroke );
0 commit comments