From 1098e2bb7dd024944a98d2672bb490cc6e76f00b Mon Sep 17 00:00:00 2001 From: Kyle LeNeau Date: Tue, 3 Jan 2012 21:37:42 -0600 Subject: [PATCH] Working on the drawing and rendering of cells. --- Classes/NSDate+iMontly.h | 3 +- Classes/NSDate+iMontly.m | 4 +- Classes/iMonthlyCommon.h | 1 + Classes/iMonthlyCommon.m | 5 ++ Classes/iMonthlyDayCellView.m | 56 +++++++++++++------ Classes/iMonthlyGridView.h | 2 + Classes/iMonthlyGridView.m | 81 +++++++++++++++++++++++++++- Classes/iMonthlyView.h | 2 + Classes/iMonthlyView.m | 26 +++++++-- Examples/Basic/BasicViewController.m | 2 +- 10 files changed, 155 insertions(+), 27 deletions(-) diff --git a/Classes/NSDate+iMontly.h b/Classes/NSDate+iMontly.h index 972cb91..fa7bebc 100644 --- a/Classes/NSDate+iMontly.h +++ b/Classes/NSDate+iMontly.h @@ -17,11 +17,12 @@ - (NSDate *)previousMonth; - (NSDate *)nextMonth; - (NSDate *)monthFromMonthOffset:(int)monthOffset; + - (NSDate *)dateWithDayNumber:(int)dayNumber; - (NSDate *)dateWithDayOffset:(int)dayOffset; - (NSInteger)firstWeekdayOfMonth; -- (NSInteger)lastDayOfMonth; +- (NSInteger)daysInMonth; - (NSInteger)visibleWeeksInMonth; - (BOOL)isSameDate:(NSDate *)day; diff --git a/Classes/NSDate+iMontly.m b/Classes/NSDate+iMontly.m index da897f7..4dff4e9 100644 --- a/Classes/NSDate+iMontly.m +++ b/Classes/NSDate+iMontly.m @@ -116,7 +116,7 @@ - (NSInteger)firstWeekdayOfMonth return [[calendar components: NSWeekdayCalendarUnit fromDate:self] weekday]; } -- (NSInteger)lastDayOfMonth +- (NSInteger)daysInMonth { NSCalendar * calendar = [NSCalendar currentCalendar]; NSRange daysRange = [calendar rangeOfUnit:NSDayCalendarUnit @@ -128,7 +128,7 @@ - (NSInteger)lastDayOfMonth - (NSInteger)visibleWeeksInMonth { NSInteger firstDay = [self firstWeekdayOfMonth] - 1; - double count = firstDay + [self lastDayOfMonth]; + double count = firstDay + [self daysInMonth]; return ceilf(count / 7); } diff --git a/Classes/iMonthlyCommon.h b/Classes/iMonthlyCommon.h index bb052fe..ece2ba3 100644 --- a/Classes/iMonthlyCommon.h +++ b/Classes/iMonthlyCommon.h @@ -13,6 +13,7 @@ @end +CGRect rectByChangingSize(CGRect rect, CGFloat deltaWidth, CGFloat deltaHeight); CGRect rectFor1PxStroke(CGRect rect); void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor); void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor); \ No newline at end of file diff --git a/Classes/iMonthlyCommon.m b/Classes/iMonthlyCommon.m index 13ca63a..5fd8824 100644 --- a/Classes/iMonthlyCommon.m +++ b/Classes/iMonthlyCommon.m @@ -12,6 +12,11 @@ @implementation iMonthlyCommon @end +CGRect rectByChangingSize(CGRect rect, CGFloat deltaWidth, CGFloat deltaHeight) +{ + return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width + deltaWidth, rect.size.height + deltaHeight); +} + CGRect rectFor1PxStroke(CGRect rect) { return CGRectMake(rect.origin.x + 0.5, rect.origin.y + 0.5, rect.size.width - 1, rect.size.height - 1); diff --git a/Classes/iMonthlyDayCellView.m b/Classes/iMonthlyDayCellView.m index 0c88fd2..9883d7d 100644 --- a/Classes/iMonthlyDayCellView.m +++ b/Classes/iMonthlyDayCellView.m @@ -42,20 +42,28 @@ - (id)initWithFrame:(CGRect)frame _font = [UIFont boldSystemFontOfSize:24.f]; _darkColor = [UIColor darkTextColor]; _whiteColor = [UIColor whiteColor]; - _lightColor = [UIColor lightTextColor]; + _lightColor = [UIColor lightGrayColor]; CGRect _labelRect = CGRectMake((frame.size.width - kLabelSize.width) / 2, 8, kLabelSize.width, kLabelSize.height); _dayNumberLabel = [[UILabel alloc] initWithFrame:_labelRect]; _dayNumberLabel.backgroundColor = [UIColor clearColor]; _dayNumberLabel.textAlignment = UITextAlignmentCenter; _dayNumberLabel.font = _font; - _dayNumberLabel.text = @"2"; + _dayNumberLabel.text = [NSString stringWithFormat:@"%u", [_date dayNumber]]; [self setDayCellState:kDayCellStateUnKnown]; [self addSubview:_dayNumberLabel]; } return self; } +- (void)setDate:(NSDate *)date +{ + _date = Nil; + _date = date; + + _dayNumberLabel.text = [NSString stringWithFormat:@"%u", [_date dayNumber]]; +} + - (void)setDayCellState:(kDayCellState)state { _cellState = state; @@ -70,11 +78,17 @@ - (void)setDayCellState:(kDayCellState)state _dayNumberLabel.textColor = _whiteColor; _dayNumberLabel.shadowColor = _darkColor; _dayNumberLabel.shadowOffset = CGSizeMake(0, 1); + + CGRect todayRect = rectByChangingSize(self.frame, 1, 1); + self.frame = todayRect; break; case kDayCellStateSelected: _dayNumberLabel.textColor = _whiteColor; _dayNumberLabel.shadowColor = _darkColor; _dayNumberLabel.shadowOffset = CGSizeMake(0, -1); + + CGRect selectedRect = rectByChangingSize(self.frame, 1, 1); + self.frame = selectedRect; break; case kDayCellStateInMonth: default: @@ -85,20 +99,28 @@ - (void)setDayCellState:(kDayCellState)state } } -//- (void)drawRect:(CGRect)rect -//{ -// CGContextRef context = UIGraphicsGetCurrentContext(); -// -// // Add a color for red up where the colors are -// [[UIColor colorWithRed:154.0/255.0 green:158.0/255.0 blue:167.0/255.0 alpha:0.75] setStroke]; -// -// CGContextSaveGState(context); -// CGContextMoveToPoint(context, 0, 0.5); -// CGContextAddLineToPoint(context, rect.size.width - 1 + 0.5, 0 + 0.5); -// CGContextAddLineToPoint(context, rect.size.width - 1 + 0.5, rect.size.height + 0.5); -// CGContextSetLineWidth(context, 1.0); -// CGContextStrokePath(context); -// CGContextRestoreGState(context); -//} +- (void)drawRect:(CGRect)rect +{ + CGContextRef context = UIGraphicsGetCurrentContext(); + + if (_cellState == kDayCellStateToday) { + [[UIColor colorWithRed:54.0/255.0 green:79.0/255.0 blue:114.0/255.0 alpha:0.8] setFill]; + CGContextFillRect(context, rect); + + CGRect inner = CGRectInset(rect, 1, 1); + [[UIColor colorWithRed:250.0/255.0 green:250.0/255.0 blue:250.0/255.0 alpha:0.4] setFill]; + CGContextFillRect(context, inner); + } + + if (_cellState == kDayCellStateSelected) { + CGColorRef strokeColor = [UIColor colorWithRed:41.0/255.0 green:54.0/255.0 blue:73.0/255.0 alpha:1.0].CGColor; + CGColorRef topColor = [UIColor colorWithRed:0/255.0 green:114.0/255.0 blue:226.0/255.0 alpha:1.0].CGColor; + CGColorRef bottomColor = [UIColor colorWithRed:0/255.0 green:114.0/255.0 blue:226.0/255.0 alpha:1.0].CGColor; + + drawGlossAndGradient(context, rect, topColor, bottomColor); + CGContextSetStrokeColorWithColor(context, strokeColor); + CGContextStrokeRect(context, rectFor1PxStroke(rect)); + } +} @end diff --git a/Classes/iMonthlyGridView.h b/Classes/iMonthlyGridView.h index 9584fa2..fb57113 100644 --- a/Classes/iMonthlyGridView.h +++ b/Classes/iMonthlyGridView.h @@ -10,4 +10,6 @@ @interface iMonthlyGridView : UIView +@property (nonatomic, strong) NSDate * currentMonth; + @end diff --git a/Classes/iMonthlyGridView.m b/Classes/iMonthlyGridView.m index fd8fa7f..591cf71 100644 --- a/Classes/iMonthlyGridView.m +++ b/Classes/iMonthlyGridView.m @@ -8,10 +8,23 @@ #import "iMonthlyGridView.h" #import "iMonthlyDayCellView.h" +#import "iMonthlyCommon.h" static const CGSize kDayCellSize = { 46.f, 44.f }; @implementation iMonthlyGridView +{ + NSDate * _today; + NSInteger _visibleWeeks; + NSInteger _firstWeekdayInMonth; + NSInteger _daysInMonth; + NSInteger _lastDayPreviousMonth; + + iMonthlyDayCellView * _selectedDayCell; +} + +@synthesize currentMonth = _currentMonth; + - (id)initWithFrame:(CGRect)frame { @@ -21,16 +34,82 @@ - (id)initWithFrame:(CGRect)frame self.opaque = NO; self.clipsToBounds = YES; self.backgroundColor = [UIColor clearColor]; + _today = [NSDate date]; + + NSInteger temp = 0; for (int i=0; i<6; i++) { for (int j=0; j<7; j++) { CGRect r = CGRectMake(j*kDayCellSize.width, i*kDayCellSize.height, kDayCellSize.width, kDayCellSize.height); - [self addSubview:[[iMonthlyDayCellView alloc] initWithFrame:r]]; + [self insertSubview:[[iMonthlyDayCellView alloc] initWithFrame:r] atIndex:temp]; + temp++; } } } return self; } +- (void)setCurrentMonth:(NSDate *)month +{ + _currentMonth = Nil; + _currentMonth = [month dateWithDayNumber:1]; + NSLog(@"%@", _currentMonth); + + _today = [NSDate date]; + _visibleWeeks = [_currentMonth visibleWeeksInMonth]; + _firstWeekdayInMonth = [_currentMonth firstWeekdayOfMonth]; + _daysInMonth = [_currentMonth daysInMonth]; + _lastDayPreviousMonth = [[_currentMonth previousMonth] daysInMonth]; + + [self setNeedsDisplay]; +} + +- (void)layoutSubviews +{ + if (_currentMonth == Nil) { + return; + } + + // Only adjust the Grid frame if it needs it + CGRect newRect = self.frame; + newRect.size.height = [_currentMonth visibleWeeksInMonth] * kDayCellSize.height; + if (newRect.size.height != self.frame.size.height) { + self.frame = newRect; + } + + + NSInteger totalCells = _visibleWeeks * 7; + iMonthlyDayCellView * dayCell = Nil; + NSMutableArray * viewsToFront = [NSMutableArray array]; + + for (int x = 0; x < totalCells; x++) { + dayCell = (iMonthlyDayCellView *)[self.subviews objectAtIndex:x]; + + NSInteger offset = x - (_firstWeekdayInMonth - 1); + NSDate * thisDate = [_currentMonth dateWithDayOffset:offset]; + [dayCell setDate:thisDate]; + + if ([_currentMonth monthContainsDay:thisDate]) { + [dayCell setDayCellState:kDayCellStateInMonth]; + } else { + [dayCell setDayCellState:kDayCellStateOutOfMonth]; + } + + if (_currentMonth && [thisDate isSameDate:_today]) { + [viewsToFront addObject:dayCell]; + [dayCell setDayCellState:kDayCellStateToday]; + } + + if (_selectedDayCell != Nil && _selectedDayCell == dayCell) { + [viewsToFront addObject:dayCell]; + [dayCell setDayCellState:kDayCellStateSelected]; + } + } + + for (UIView * cell in viewsToFront) { + [self bringSubviewToFront:cell]; + } +} + - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); diff --git a/Classes/iMonthlyView.h b/Classes/iMonthlyView.h index c9dff6f..30b5590 100644 --- a/Classes/iMonthlyView.h +++ b/Classes/iMonthlyView.h @@ -25,4 +25,6 @@ } +@property (nonatomic, strong) NSDate * currentMonth; + @end \ No newline at end of file diff --git a/Classes/iMonthlyView.m b/Classes/iMonthlyView.m index 085ee2d..bc8af4d 100644 --- a/Classes/iMonthlyView.m +++ b/Classes/iMonthlyView.m @@ -32,10 +32,14 @@ @implementation iMonthlyView iMonthlyGridView * _backGridView; } +@synthesize currentMonth = _currentMonth; + + - (void)initView { self.autoresizesSubviews = YES; - self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin; + 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); @@ -50,7 +54,7 @@ - (void)initView _headerTitleLabel.textAlignment = UITextAlignmentCenter; _headerTitleLabel.shadowColor = [UIColor whiteColor]; _headerTitleLabel.shadowOffset = CGSizeMake(0, 1); - _headerTitleLabel.text = @"December 2011"; // TODO with logic + _headerTitleLabel.text = [_currentMonth formattedMonthYearString]; [self addSubview:_headerTitleLabel]; NSArray * weekdayNames = [[[NSDateFormatter alloc] init] shortWeekdaySymbols]; @@ -72,13 +76,14 @@ - (void)initView // 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(@"First Weekday of Month: %d", [[[NSDate date] nextMonth] firstWeekdayOfMonth]); - NSLog(@"Visible Weeks: %d", [[[NSDate date] monthFromMonthOffset:-3] visibleWeeksInMonth]); + NSLog(@"Visible Weeks: %d", [_currentMonth visibleWeeksInMonth]); } - (id)initWithCoder:(NSCoder *)aDecoder @@ -97,6 +102,17 @@ - (id)initWithFrame:(CGRect)frame 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(); diff --git a/Examples/Basic/BasicViewController.m b/Examples/Basic/BasicViewController.m index b13bc41..936d99a 100644 --- a/Examples/Basic/BasicViewController.m +++ b/Examples/Basic/BasicViewController.m @@ -30,7 +30,7 @@ - (void)didReceiveMemoryWarning - (void)loadView { self.view = [[UIView alloc] init]; - self.calendar = [[iMonthlyView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; + self.calendar = [[iMonthlyView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)]; [self.view addSubview:self.calendar]; }