This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
/
iMonthlyView.m
153 lines (120 loc) · 5.01 KB
/
iMonthlyView.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
//
// iMonthlyView.m
// iMonthly
//
// Created by Kyle LeNeau on 12/26/11.
// Copyright (c) 2011 LeNeau Software. All rights reserved.
//
#import "iMonthlyCommon.h"
#import "iMonthlyView.h"
#import "iMonthlyGridView.h"
static const CGFloat kHeaderHeight = 44.f;
static const CGFloat kMonthLabelWidth = 240.0f;
static const CGFloat kMonthLabelHeight = 17.f;
static const CGFloat kChangeMonthButtonWidth = 46.0f;
static const CGFloat kChangeMonthButtonHeight = 30.0f;
@implementation iMonthlyView
{
CGRect _headerRect;
CGRect _gridRect;
UIColor * _headerTextColor;
UILabel * _headerTitleLabel;
UIButton * _nextMonthButton;
UIButton * _previousMonthButton;
iMonthlyGridView * _frontGridView;
iMonthlyGridView * _backGridView;
}
@synthesize currentMonth = _currentMonth;
- (void)initView
{
self.autoresizesSubviews = YES;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.currentMonth = [NSDate date];
_headerRect = CGRectMake(0, 0, self.frame.size.width, kHeaderHeight);
_gridRect = CGRectMake(0, kHeaderHeight, self.frame.size.width, self.frame.size.height - kHeaderHeight);
_headerTextColor = [UIColor colorWithRed:84.0/255.0 green:84.0/255.0 blue:84.0/255.0 alpha:1.0];
// Setup Header Views
_headerTitleLabel = [[UILabel alloc] init];
_headerTitleLabel.backgroundColor = [UIColor clearColor];
_headerTitleLabel.font = [UIFont boldSystemFontOfSize:22.f];
_headerTitleLabel.textColor = _headerTextColor;
_headerTitleLabel.textAlignment = UITextAlignmentCenter;
_headerTitleLabel.shadowColor = [UIColor whiteColor];
_headerTitleLabel.shadowOffset = CGSizeMake(0, 1);
_headerTitleLabel.text = [_currentMonth formattedMonthYearString];
[self addSubview:_headerTitleLabel];
NSArray * weekdayNames = [[[NSDateFormatter alloc] init] shortWeekdaySymbols];
NSUInteger firstWeekday = [[NSCalendar currentCalendar] firstWeekday];
NSUInteger i = firstWeekday - 1;
for (CGFloat xOffset = 0.f; xOffset < _headerRect.size.width; xOffset += 46.f, i = (i+1)%7) {
CGRect weekdayFrame = CGRectMake(xOffset, 30.f, 46.f, kHeaderHeight - 29.f);
UILabel * weekdayLabel = [[UILabel alloc] initWithFrame:weekdayFrame];
weekdayLabel.backgroundColor = [UIColor clearColor];
weekdayLabel.font = [UIFont boldSystemFontOfSize:10.f];
weekdayLabel.textAlignment = UITextAlignmentCenter;
weekdayLabel.textColor = _headerTextColor;
weekdayLabel.shadowColor = [UIColor whiteColor];
weekdayLabel.shadowOffset = CGSizeMake(0, 1);
weekdayLabel.text = [weekdayNames objectAtIndex:i];
[self addSubview:weekdayLabel];
}
// Setup the Grid Views
_frontGridView = [[iMonthlyGridView alloc] initWithFrame:_gridRect];
_frontGridView.currentMonth = _currentMonth;
[_frontGridView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:NULL];
_backGridView = [[iMonthlyGridView alloc] initWithFrame:_gridRect];
[self addSubview:_frontGridView];
NSLog(@"Visible Weeks: %d", [_currentMonth visibleWeeksInMonth]);
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder])) {
[self initView];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self initView];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == _frontGridView && [keyPath isEqualToString:@"frame"]) {
NSLog(@"Recieved a frame change message");
CGRect newFrame = self.frame;
newFrame.size.height = _headerRect.size.height + _frontGridView.frame.size.height;
self.frame = newFrame;
}
}
- (void)drawHeaderView
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef topColor = [UIColor colorWithRed:246.0/255.0 green:246.0/255.0 blue:247.0/255.0 alpha:1.0].CGColor;
CGColorRef bottomColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:209.0/255.0 alpha:1.0].CGColor;
drawLinearGradient(context, _headerRect, topColor, bottomColor);
}
- (void)drawGridView
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef topColor = [UIColor colorWithRed:226.0/255.0 green:226.0/255.0 blue:228.0/255.0 alpha:1.0].CGColor;
CGColorRef bottomColor = [UIColor colorWithRed:204.0/255.0 green:203.0/255.0 blue:208.0/255.0 alpha:1.0].CGColor;
drawLinearGradient(context, _gridRect, topColor, bottomColor);
}
- (void)drawRect:(CGRect)rect
{
[self drawHeaderView];
[self drawGridView];
}
- (void) layoutSubviews
{
[super layoutSubviews];
_previousMonthButton.center = CGPointMake(20, 20);
_nextMonthButton.center = CGPointMake(300, 20);
_headerTitleLabel.frame = CGRectMake(40, 9, kMonthLabelWidth, kMonthLabelHeight);
// _daysGridRect = CGRectMake(0, 70, 320, 300);
[self setNeedsDisplay];
}
@end