-
Notifications
You must be signed in to change notification settings - Fork 0
/
KalMonthView.m
executable file
·128 lines (111 loc) · 3.95 KB
/
KalMonthView.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
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import <CoreGraphics/CoreGraphics.h>
#import "KalMonthView.h"
#import "KalTileView.h"
#import "KalView.h"
#import "KalPrivate.h"
extern const CGSize kTileSize;
@implementation KalMonthView
@synthesize numWeeks;
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
tileAccessibilityFormatter = [[NSDateFormatter alloc] init];
[tileAccessibilityFormatter setDateFormat:@"eeee, MMMM d"];
[tileAccessibilityFormatter setMonthSymbols:@[@"1月",@"2月",@"3月",@"4月",@"5月",@"6月",@"7月",@"8月",@"9月",@"10月",@"11月",@"12月",]];
self.opaque = NO;
self.clipsToBounds = YES;
for (int i=0; i<6; i++) {
for (int j=0; j<7; j++) {
CGRect r = CGRectMake(j*kTileSize.width, i*kTileSize.height, kTileSize.width, kTileSize.height);
[self addSubview:[[KalTileView alloc] initWithFrame:r]];
}
}
}
return self;
}
- (void)showDates:(NSArray *)mainDates leadingAdjacentDates:(NSArray *)leadingAdjacentDates trailingAdjacentDates:(NSArray *) trailingAdjacentDates minAvailableDate:(NSDate *)minAvailableDate maxAvailableDate:(NSDate *)maxAvailableDate
{
int tileNum = 0;
NSArray *dates[] = { leadingAdjacentDates, mainDates, trailingAdjacentDates };
for (int i=0; i<3; i++) {
for (int j=0; j<dates[i].count; j++) {
NSDate *d = dates[i][j];
KalTileView *tile = [self.subviews objectAtIndex:tileNum];
[tile resetState];
tile.date = d;
if ((minAvailableDate && [d compare:minAvailableDate] == NSOrderedAscending) || (maxAvailableDate && [d compare:maxAvailableDate] == NSOrderedDescending)) {
tile.type = KalTileTypeDisable;
}
if (i == 0 && j == 0) {
tile.type |= KalTileTypeFirst;
}
if (i == 2 && j == dates[i].count-1) {
tile.type |= KalTileTypeLast;
}
if (dates[i] != mainDates) {
tile.type |= KalTileTypeAdjacent;
}
if ([d isToday]) {
tile.type |= KalTileTypeToday;
}
tileNum++;
}
}
numWeeks = ceilf(tileNum / 7.f);
[self sizeToFit];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextDrawTiledImage(ctx, (CGRect){CGPointZero,kTileSize}, [[UIImage imageNamed:@"Kal.bundle/kal_tile.png"] CGImage]);
}
- (KalTileView *)firstTileOfMonth
{
KalTileView *tile = nil;
for (KalTileView *t in self.subviews) {
if (!t.belongsToAdjacentMonth) {
tile = t;
break;
}
}
return tile;
}
- (KalTileView *)tileForDate:(NSDate *)date
{
KalTileView *tile = nil;
for (KalTileView *t in self.subviews) {
if ([t.date isEqualToDate:date]) {
tile = t;
break;
}
}
return tile;
}
- (void)sizeToFit
{
self.height = 1.f + kTileSize.height * numWeeks;
}
- (void)markTilesForDates:(NSArray *)dates
{
for (KalTileView *tile in self.subviews)
{
tile.marked = [dates containsObject:tile.date];
NSString *dayString = [tileAccessibilityFormatter stringFromDate:tile.date];
if (dayString) {
NSMutableString *helperText = [[NSMutableString alloc] initWithCapacity:128];
if ([tile.date isToday])
[helperText appendFormat:@"%@ ", NSLocalizedString(@"Today", @"Accessibility text for a day tile that represents today")];
[helperText appendString:dayString];
if (tile.marked)
[helperText appendFormat:@". %@", NSLocalizedString(@"Marked", @"Accessibility text for a day tile which is marked with a small dot")];
[tile setAccessibilityLabel:helperText];
}
}
}
#pragma mark -
@end