forked from Awful/Awful.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwfulBadgeView.m
124 lines (106 loc) · 3.48 KB
/
AwfulBadgeView.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
//
// AwfulBadgeView.m
// Awful
//
// Created by Nolan Waite on 2012-10-02.
// Copyright (c) 2012 Regular Berry Software LLC. All rights reserved.
//
#import "AwfulBadgeView.h"
@interface AwfulBadgeView ()
@property (weak, nonatomic) UITableViewCell *cell;
@property (readonly, nonatomic) UIFont *font;
@end
@implementation AwfulBadgeView
- (id)initWithCell:(UITableViewCell *)cell
{
self = [super initWithFrame:CGRectZero];
if (self) {
_cell = cell;
_textColor = [UIColor whiteColor];
_highlightedTextColor = [UIColor blackColor];
_badgeColor = [UIColor colorWithRed:0.169 green:0.408 blue:0.588 alpha:1];
_highlightedBadgeColor = [UIColor whiteColor];
_offBadgeColor = [UIColor colorWithRed:0.435 green:0.659 blue:0.769 alpha:1];
_on = YES;
self.backgroundColor = [UIColor clearColor];
self.contentMode = UIViewContentModeRedraw;
self.layer.masksToBounds = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *badgeColor = self.on ? self.badgeColor : self.offBadgeColor;
UIColor *textColor = self.textColor;
if (self.cell.highlighted || self.cell.selected) {
badgeColor = self.highlightedBadgeColor;
textColor = self.highlightedTextColor;
}
CGContextSetFillColorWithColor(context, badgeColor.CGColor);
CGContextAddArc(context,
self.bounds.origin.x + self.bounds.size.width - self.bounds.size.height / 2,
self.bounds.origin.y + self.bounds.size.height / 2,
self.bounds.size.height / 2, M_PI / 2, M_PI * 3 / 2, 1);
CGContextAddArc(context,
self.bounds.origin.x + self.bounds.size.height / 2,
self.bounds.origin.y + self.bounds.size.height / 2,
self.bounds.size.height / 2, M_PI * 3 / 2, M_PI / 2, 1);
CGContextDrawPath(context, kCGPathFill);
CGContextSetFillColorWithColor(context, textColor.CGColor);
[self.badgeText drawInRect:CGRectInset(self.bounds, 7, 2) withFont:self.font];
}
- (void)sizeToFit
{
CGSize textSize = [self.badgeText sizeWithFont:self.font];
CGRect bounds = self.bounds;
bounds.size = CGSizeMake(textSize.width + 14, textSize.height + 4);
self.bounds = bounds;
}
- (UIFont *)font
{
return [UIFont boldSystemFontOfSize:13];
}
- (void)setBadgeText:(NSString *)badgeText
{
if (_badgeText == badgeText) return;
_badgeText = [badgeText copy];
[self setNeedsDisplay];
}
- (void)setTextColor:(UIColor *)textColor
{
if (_textColor == textColor) return;
_textColor = textColor;
[self setNeedsDisplay];
}
- (void)setHighlightedTextColor:(UIColor *)highlightedTextColor
{
if (_highlightedTextColor == highlightedTextColor) return;
_highlightedTextColor = highlightedTextColor;
[self setNeedsDisplay];
}
- (void)setBadgeColor:(UIColor *)badgeColor
{
if (_badgeColor == badgeColor) return;
_badgeColor = badgeColor;
[self setNeedsDisplay];
}
- (void)setHighlightedBadgeColor:(UIColor *)highlightedBadgeColor
{
if (_highlightedBadgeColor == highlightedBadgeColor) return;
_highlightedBadgeColor = highlightedBadgeColor;
[self setNeedsDisplay];
}
- (void)setOffBadgeColor:(UIColor *)offBadgeColor
{
if (_offBadgeColor == offBadgeColor) return;
_offBadgeColor = offBadgeColor;
[self setNeedsDisplay];
}
- (void)setOn:(BOOL)on
{
if (_on == on) return;
_on = on;
[self setNeedsDisplay];
}
@end