-
Notifications
You must be signed in to change notification settings - Fork 0
/
KalView.m
executable file
·205 lines (170 loc) · 9.25 KB
/
KalView.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
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import "KalView.h"
#import "KalGridView.h"
#import "KalLogic.h"
#import "KalPrivate.h"
@interface KalView ()
- (void)addSubviewsToHeaderView:(UIView *)headerView;
- (void)addSubviewsToContentView:(UIView *)contentView;
- (void)setHeaderTitleText:(NSString *)text;
@end
static const CGFloat kHeaderHeight = 68.f;
static const CGFloat kMonthLabelHeight = 17.f;
@implementation KalView
- (id)initWithFrame:(CGRect)frame delegate:(id<KalViewDelegate>)theDelegate logic:(KalLogic *)theLogic
{
if ((self = [super initWithFrame:frame])) {
self.delegate = theDelegate;
logic = theLogic;
[logic addObserver:self forKeyPath:@"selectedMonthNameAndYear" options:NSKeyValueObservingOptionNew context:NULL];
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.backgroundColor = RGBCOLOR(246, 246, 246);
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, frame.size.width, kHeaderHeight)];
[self addSubviewsToHeaderView:headerView];
[self addSubview:headerView];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0.f, kHeaderHeight, frame.size.width, frame.size.height - kHeaderHeight)];
contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self addSubviewsToContentView:contentView];
[self addSubview:contentView];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
[NSException raise:@"Incomplete initializer" format:@"KalView must be initialized with a delegate and a KalLogic. Use the initWithFrame:delegate:logic: method."];
return nil;
}
- (void)redrawEntireMonth { [self jumpToSelectedMonth]; }
- (void)slideDown { [self.gridView slideDown]; }
- (void)slideUp { [self.gridView slideUp]; }
- (void)showPreviousMonth
{
if (!self.gridView.transitioning)
[self.delegate showPreviousMonth];
}
//- (void)showFollowingMonth
//{
// if (!self.gridView.transitioning)
// [self.delegate showFollowingMonth];
//}
- (void)addSubviewsToHeaderView:(UIView *)headerView
{
// const CGFloat kChangeMonthButtonWidth = 46.0f;
// const CGFloat kChangeMonthButtonHeight = 30.0f;
const CGFloat kMonthLabelWidth = 200.0f;
const CGFloat kHeaderVerticalAdjust = 13.f;
// Create the previous month button on the left side of the view
// CGRect previousMonthButtonFrame = CGRectMake(self.left,
// kHeaderVerticalAdjust,
// kChangeMonthButtonWidth,
// kChangeMonthButtonHeight);
// UIButton *previousMonthButton = [[UIButton alloc] initWithFrame:previousMonthButtonFrame];
// [previousMonthButton setAccessibilityLabel:NSLocalizedString(@"Previous month", nil)];
// [previousMonthButton setImage:[UIImage imageNamed:@"Kal.bundle/kal_left_arrow.png"] forState:UIControlStateNormal];
// previousMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// previousMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// [previousMonthButton addTarget:self action:@selector(showPreviousMonth) forControlEvents:UIControlEventTouchUpInside];
// [headerView addSubview:previousMonthButton];
// Draw the selected month name centered and at the top of the view
CGRect monthLabelFrame = CGRectMake((self.width/2.0f) - (kMonthLabelWidth/2.0f),
kHeaderVerticalAdjust,
kMonthLabelWidth,
kMonthLabelHeight);
headerTitleLabel = [[UILabel alloc] initWithFrame:monthLabelFrame];
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:22];
headerTitleLabel.textAlignment = NSTextAlignmentCenter;
headerTitleLabel.textColor = kDarkGrayColor;
[self setHeaderTitleText:[logic selectedMonthNameAndYear]];
[headerView addSubview:headerTitleLabel];
// Create the next month button on the right side of the view
// CGRect nextMonthButtonFrame = CGRectMake(self.width - kChangeMonthButtonWidth,
// kHeaderVerticalAdjust,
// kChangeMonthButtonWidth,
// kChangeMonthButtonHeight);
// UIButton *nextMonthButton = [[UIButton alloc] initWithFrame:nextMonthButtonFrame];
// [nextMonthButton setAccessibilityLabel:NSLocalizedString(@"Next month", nil)];
// [nextMonthButton setImage:[UIImage imageNamed:@"Kal.bundle/kal_right_arrow.png"] forState:UIControlStateNormal];
// nextMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// nextMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// [nextMonthButton addTarget:self action:@selector(showFollowingMonth) forControlEvents:UIControlEventTouchUpInside];
// [headerView addSubview:nextMonthButton];
// Add column labels for each weekday (adjusting based on the current locale's first weekday)
NSDateFormatter *dateform =[[NSDateFormatter alloc]init];
[dateform setWeekdaySymbols:@[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",]];
NSArray *weekdayNames = [dateform weekdaySymbols];
NSArray *fullWeekdayNames = [[[NSDateFormatter alloc] init] standaloneWeekdaySymbols];
NSUInteger firstWeekday = [[NSCalendar currentCalendar] firstWeekday];
NSUInteger i = firstWeekday - 1;
for (CGFloat xOffset = 0.f; xOffset < headerView.width; xOffset += 46.f, i = (i+1)%7) {
CGRect weekdayFrame = CGRectMake(xOffset, 30.f, 46.f, kHeaderHeight - 15.f);
UILabel *weekdayLabel = [[UILabel alloc] initWithFrame:weekdayFrame];
weekdayLabel.backgroundColor = [UIColor clearColor];
weekdayLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
weekdayLabel.textAlignment = NSTextAlignmentCenter;
weekdayLabel.textColor = kGrayColor;
weekdayLabel.text = [weekdayNames objectAtIndex:i];
[weekdayLabel setAccessibilityLabel:[fullWeekdayNames objectAtIndex:i]];
[headerView addSubview:weekdayLabel];
}
}
- (void)addSubviewsToContentView:(UIView *)contentView
{
// Both the tile grid and the list of events will automatically lay themselves
// out to fit the # of weeks in the currently displayed month.
// So the only part of the frame that we need to specify is the width.
CGRect fullWidthAutomaticLayoutFrame = CGRectMake(0.f, 0.f, self.width, 0.f);
// The tile grid (the calendar body)
self.gridView = [[KalGridView alloc] initWithFrame:fullWidthAutomaticLayoutFrame logic:logic delegate:self.delegate];
[self.gridView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:NULL];
[contentView addSubview:self.gridView];
// The list of events for the selected day
self.tableView = [[UITableView alloc] initWithFrame:fullWidthAutomaticLayoutFrame style:UITableViewStylePlain];
// self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[contentView addSubview:self.tableView];
// Trigger the initial KVO update to finish the contentView layout
[self.gridView sizeToFit];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == self.gridView && [keyPath isEqualToString:@"frame"]) {
/* Animate tableView filling the remaining space after the
* gridView expanded or contracted to fit the # of weeks
* for the month that is being displayed.
*
* This observer method will be called when gridView's height
* changes, which we know to occur inside a Core Animation
* transaction. Hence, when I set the "frame" property on
* tableView here, I do not need to wrap it in a
* [UIView beginAnimations:context:].
*/
CGFloat gridBottom = self.gridView.top + self.gridView.height;
CGRect frame = self.tableView.frame;
frame.origin.y = gridBottom;
frame.size.height = self.tableView.superview.height - gridBottom;
self.tableView.frame = frame;
} else if ([keyPath isEqualToString:@"selectedMonthNameAndYear"]) {
[self setHeaderTitleText:[change objectForKey:NSKeyValueChangeNewKey]];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)setHeaderTitleText:(NSString *)text
{
[headerTitleLabel setText:text];
[headerTitleLabel sizeToFit];
headerTitleLabel.left = floorf(self.width/2.f - headerTitleLabel.width/2.f);
}
- (void)jumpToSelectedMonth { [self.gridView jumpToSelectedMonth]; }
- (BOOL)isSliding { return self.gridView.transitioning; }
- (void)markTilesForDates:(NSArray *)dates { [self.gridView markTilesForDates:dates]; }
- (void)dealloc
{
[logic removeObserver:self forKeyPath:@"selectedMonthNameAndYear"];
[self.gridView removeObserver:self forKeyPath:@"frame"];
}
@end