forked from klazuka/Kal
/
KalTileView.m
199 lines (165 loc) · 5.29 KB
/
KalTileView.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
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import "KalTileView.h"
#import "KalDate.h"
#import "KalPrivate.h"
extern const CGSize kTileSize;
@implementation KalTileView
@synthesize date;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = NO;
origin = frame.origin;
[self resetState];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGFloat fontSize = 24.f;
UIFont *font = [UIFont boldSystemFontOfSize:fontSize];
UIColor *shadowColor = nil;
UIColor *textColor = nil;
UIImage *markerImage = nil;
CGContextSelectFont(ctx, [font.fontName cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);
CGContextTranslateCTM(ctx, 0, kTileSize.height);
CGContextScaleCTM(ctx, 1, -1);
if ([self isToday] && self.selected) {
[[[UIImage imageNamed:@"kal_tile_today_selected.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] drawInRect:CGRectMake(0, -1, kTileSize.width+1, kTileSize.height+1)];
textColor = [UIColor whiteColor];
shadowColor = [UIColor blackColor];
markerImage = [UIImage imageNamed:@"kal_marker_today.png"];
} else if ([self isToday] && !self.selected) {
[[[UIImage imageNamed:@"kal_tile_today.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] drawInRect:CGRectMake(0, -1, kTileSize.width+1, kTileSize.height+1)];
textColor = [UIColor whiteColor];
shadowColor = [UIColor blackColor];
markerImage = [UIImage imageNamed:@"kal_marker_today.png"];
} else if (self.selected) {
[[[UIImage imageNamed:@"kal_tile_selected.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:0] drawInRect:CGRectMake(0, -1, kTileSize.width+1, kTileSize.height+1)];
textColor = [UIColor whiteColor];
shadowColor = [UIColor blackColor];
markerImage = [UIImage imageNamed:@"kal_marker_selected.png"];
} else if (self.belongsToAdjacentMonth) {
textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"kal_tile_dim_text_fill.png"]];
shadowColor = nil;
markerImage = [UIImage imageNamed:@"kal_marker_dim.png"];
} else {
textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"kal_tile_text_fill.png"]];
shadowColor = [UIColor whiteColor];
markerImage = [UIImage imageNamed:@"kal_marker.png"];
}
if (flags.marked)
[markerImage drawInRect:CGRectMake(21.f, 5.f, 4.f, 5.f)];
NSUInteger n = [self.date day];
NSString *dayText = [NSString stringWithFormat:@"%lu", (unsigned long)n];
const char *day = [dayText cStringUsingEncoding:NSUTF8StringEncoding];
CGSize textSize = [dayText sizeWithFont:font];
CGFloat textX, textY;
textX = roundf(0.5f * (kTileSize.width - textSize.width));
textY = 6.f + roundf(0.5f * (kTileSize.height - textSize.height));
if (shadowColor) {
[shadowColor setFill];
CGContextShowTextAtPoint(ctx, textX, textY, day, n >= 10 ? 2 : 1);
textY += 1.f;
}
[textColor setFill];
CGContextShowTextAtPoint(ctx, textX, textY, day, n >= 10 ? 2 : 1);
if (self.highlighted) {
[[UIColor colorWithWhite:0.25f alpha:0.3f] setFill];
CGContextFillRect(ctx, CGRectMake(0.f, 0.f, kTileSize.width, kTileSize.height));
}
}
- (void)resetState
{
// realign to the grid
CGRect frame = self.frame;
frame.origin = origin;
frame.size = kTileSize;
self.frame = frame;
[date release];
date = nil;
flags.type = KalTileTypeRegular;
flags.highlighted = NO;
flags.selected = NO;
flags.marked = NO;
}
- (void)setDate:(KalDate *)aDate
{
if (date == aDate)
return;
[date release];
date = [aDate retain];
[self setNeedsDisplay];
}
- (BOOL)isSelected { return flags.selected; }
- (void)setSelected:(BOOL)selected
{
if (flags.selected == selected)
return;
// workaround since I cannot draw outside of the frame in drawRect:
if (![self isToday]) {
CGRect rect = self.frame;
if (selected) {
rect.origin.x--;
rect.size.width++;
rect.size.height++;
} else {
rect.origin.x++;
rect.size.width--;
rect.size.height--;
}
self.frame = rect;
}
flags.selected = selected;
[self setNeedsDisplay];
}
- (BOOL)isHighlighted { return flags.highlighted; }
- (void)setHighlighted:(BOOL)highlighted
{
if (flags.highlighted == highlighted)
return;
flags.highlighted = highlighted;
[self setNeedsDisplay];
}
- (BOOL)isMarked { return flags.marked; }
- (void)setMarked:(BOOL)marked
{
if (flags.marked == marked)
return;
flags.marked = marked;
[self setNeedsDisplay];
}
- (KalTileType)type { return flags.type; }
- (void)setType:(KalTileType)tileType
{
if (flags.type == tileType)
return;
// workaround since I cannot draw outside of the frame in drawRect:
CGRect rect = self.frame;
if (tileType == KalTileTypeToday) {
rect.origin.x--;
rect.size.width++;
rect.size.height++;
} else {
rect.origin.x++;
rect.size.width--;
rect.size.height--;
}
self.frame = rect;
flags.type = tileType;
[self setNeedsDisplay];
}
- (BOOL)isToday { return flags.type == KalTileTypeToday; }
- (BOOL)belongsToAdjacentMonth { return flags.type == KalTileTypeAdjacent; }
- (void)dealloc
{
[date release];
[super dealloc];
}
@end