Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Working on the drawing and rendering of cells.
  • Loading branch information
KyleLeneau committed Jan 4, 2012
1 parent 6097c30 commit 1098e2b
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Classes/NSDate+iMontly.h
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Classes/NSDate+iMontly.m
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions Classes/iMonthlyCommon.h
Expand Up @@ -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);
5 changes: 5 additions & 0 deletions Classes/iMonthlyCommon.m
Expand Up @@ -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);
Expand Down
56 changes: 39 additions & 17 deletions Classes/iMonthlyDayCellView.m
Expand Up @@ -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;
Expand All @@ -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:
Expand All @@ -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
2 changes: 2 additions & 0 deletions Classes/iMonthlyGridView.h
Expand Up @@ -10,4 +10,6 @@

@interface iMonthlyGridView : UIView

@property (nonatomic, strong) NSDate * currentMonth;

@end
81 changes: 80 additions & 1 deletion Classes/iMonthlyGridView.m
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions Classes/iMonthlyView.h
Expand Up @@ -25,4 +25,6 @@

}

@property (nonatomic, strong) NSDate * currentMonth;

@end
26 changes: 21 additions & 5 deletions Classes/iMonthlyView.m
Expand Up @@ -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);
Expand All @@ -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];
Expand All @@ -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
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Examples/Basic/BasicViewController.m
Expand Up @@ -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];
}

Expand Down

0 comments on commit 1098e2b

Please sign in to comment.