Permalink
Browse files

Added some date logic code, and a sample controller.

Started playing around with learning how to draw using core graphics.
  • Loading branch information...
1 parent 2c96631 commit 2f2a8580e80e95a507a7c3ee216d67700bea9e84 @KyleLeneau committed Dec 29, 2011
View
@@ -0,0 +1,30 @@
+//
+// NSDate+iMontly.h
+// iMonthly
+//
+// Created by Kyle LeNeau on 12/26/11.
+// Copyright (c) 2011 LeNeau Software. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSDate (iMontly)
+
+- (NSString *)formattedMonthYearString;
+- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat;
+
+- (NSDate *)thisMonth;
+- (NSDate *)previousMonth;
+- (NSDate *)nextMonth;
+- (NSDate *)monthFromMonthOffset:(int)monthOffset;
+- (NSDate *)dateWithDayNumber:(int)dayNumber;
+- (NSDate *)dateWithDayOffset:(int)dayOffset;
+
+- (NSInteger)firstWeekdayOfMonth;
+- (NSInteger)lastDayOfMonth;
+
+- (BOOL)isSameDate:(NSDate *)day;
+- (BOOL)monthContainsDay:(NSDate *)day;
+- (NSInteger)dayNumber;
+
+@end
View
@@ -0,0 +1,128 @@
+//
+// NSDate+iMontly.m
+// iMonthly
+//
+// Created by Kyle LeNeau on 12/26/11.
+// Copyright (c) 2011 LeNeau Software. All rights reserved.
+//
+
+#import "NSDate+iMontly.h"
+#define DATE_COMPONENTS NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
+
+@implementation NSDate (iMontly)
+
+- (NSString *)formattedMonthYearString
+{
+ return [self formattedStringUsingFormat:@"MMMM yyyy"];
+}
+
+- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat
+{
+ NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+ [formatter setDateFormat:dateFormat];
+ [formatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier:@"en"]];
+
+ return [formatter stringFromDate:self];
+}
+
+- (NSDate *)thisMonth
+{
+ return [self monthFromMonthOffset:0];
+}
+
+- (NSDate *)previousMonth
+{
+ return [self monthFromMonthOffset:-1];
+}
+
+- (NSDate *)nextMonth
+{
+ return [self monthFromMonthOffset:+1];
+}
+
+- (NSDate *)nextDay
+{
+ return [self dateWithDayOffset:+1];
+}
+
+- (NSDate *)dateWithDayOffset:(int)dayOffset
+{
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSDateComponents * components = [calendar components:DATE_COMPONENTS fromDate:self];
+
+ [components setDay: [components day] + dayOffset];
+ return [calendar dateFromComponents:components];
+}
+
+- (NSDate *)monthFromMonthOffset:(int)monthOffset
+{
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSDateComponents * components = [calendar components:DATE_COMPONENTS fromDate:self];
+
+ if (monthOffset == 0) {
+ [components setDay: ([components day] - ([components day] - 1))];
+ } else {
+ [components setMonth: ([components month] + monthOffset)];
+ }
+
+ return [calendar dateFromComponents:components];
+}
+
+- (BOOL)isSameDate:(NSDate *)day
+{
+ if (day == nil) {
+ return NO;
+ }
+
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSDateComponents * selfComponents = [calendar components:DATE_COMPONENTS fromDate:self];
+ NSDateComponents * dayComponents = [calendar components:DATE_COMPONENTS fromDate:day];
+
+ return (selfComponents.day == dayComponents.day) && (selfComponents.month == dayComponents.month) && (selfComponents.year == dayComponents.year);
+}
+
+- (BOOL)monthContainsDay:(NSDate *)day
+{
+ if (day == nil) {
+ return NO;
+ }
+
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSDateComponents * selfComponents = [calendar components:DATE_COMPONENTS fromDate:self];
+ NSDateComponents * dayComponents = [calendar components:DATE_COMPONENTS fromDate:day];
+
+ return (selfComponents.month == dayComponents.month) && (selfComponents.year == dayComponents.year);
+}
+
+- (NSDate *)dateWithDayNumber:(int)dayNumber
+{
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSDateComponents * components = [calendar components:DATE_COMPONENTS fromDate:self];
+ [components setDay: dayNumber];
+
+ return [calendar dateFromComponents:components];
+}
+
+- (NSInteger)dayNumber
+{
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSDateComponents * components = [calendar components:DATE_COMPONENTS fromDate:self];
+ return components.day;
+}
+
+- (NSInteger)firstWeekdayOfMonth
+{
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ return [[calendar components: NSWeekdayCalendarUnit fromDate:self] weekday];
+}
+
+- (NSInteger)lastDayOfMonth
+{
+ NSCalendar * calendar = [NSCalendar currentCalendar];
+ NSRange daysRange = [calendar rangeOfUnit:NSDayCalendarUnit
+ inUnit:NSMonthCalendarUnit
+ forDate:self];
+ return daysRange.length;
+}
+
+@end
View
@@ -7,3 +7,5 @@
//
+#import "NSDate+iMontly.h"
+#import "iMonthlyView.h"
View
@@ -0,0 +1,29 @@
+//
+// iMonthlyView.h
+// iMonthly
+//
+// Created by Kyle LeNeau on 12/26/11.
+// Copyright (c) 2011 LeNeau Software. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+@class iMonthlyView;
+
+
+@protocol iMonthlyViewDelegate
+
+@optional
+- (void)imonthlyView:(iMonthlyView *)view didSelectDate:(NSDate *)selectedDate;
+- (void)imonthlyView:(iMonthlyView *)view didMoveToNextMonth:(NSDate *)newMonth;
+- (void)imonthlyView:(iMonthlyView *)view didMoveToPreviousMonth:(NSDate *)newMonth;
+
+@end
+
+@interface iMonthlyView : UIView {
+
+}
+
+@end
+
+
+void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor);
View
@@ -0,0 +1,85 @@
+//
+// iMonthlyView.m
+// iMonthly
+//
+// Created by Kyle LeNeau on 12/26/11.
+// Copyright (c) 2011 LeNeau Software. All rights reserved.
+//
+
+#import "iMonthlyView.h"
+
+@implementation iMonthlyView
+{
+ CGRect _headerRect;
+ CGRect _gridRect;
+}
+
+- (void)initView
+{
+ _headerRect = CGRectMake(0, 0, 320, 44);
+}
+
+- (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;
+}
+
+// Only override drawRect: if you perform custom drawing.
+// An empty implementation adversely affects performance during animation.
+- (void)drawRect:(CGRect)rect
+{
+ // Drawing code
+ CGContextRef context = UIGraphicsGetCurrentContext();
+
+ CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
+ CGContextFillRect(context, self.bounds);
+
+
+ CGColorRef whiteColor = [UIColor colorWithRed:1.0 green:1.0
+ blue:1.0 alpha:1.0].CGColor;
+ CGColorRef lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0
+ blue:230.0/255.0 alpha:1.0].CGColor;
+
+ drawLinearGradient(context, _headerRect, whiteColor, lightGrayColor);
+
+ [@"December 2011" drawInRect:CGRectMake(0, 0, 320, 12)
+ withFont:[UIFont boldSystemFontOfSize:22]
+ lineBreakMode:UILineBreakModeClip
+ alignment:UITextAlignmentCenter];
+}
+
+@end
+
+
+void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
+{
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGFloat locations[] = { 0.0, 1.0 };
+
+ NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
+
+ CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
+
+ CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
+ CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
+
+ CGContextSaveGState(context);
+ CGContextAddRect(context, rect);
+ CGContextClip(context);
+ CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
+ CGContextRestoreGState(context);
+
+ CGGradientRelease(gradient);
+ CGColorSpaceRelease(colorSpace);
+}
@@ -7,7 +7,10 @@
//
#import <UIKit/UIKit.h>
+#import "iMonthly.h"
@interface BasicViewController : UIViewController
+@property (nonatomic, strong) iMonthlyView *calendar;
+
@end
@@ -10,10 +10,11 @@
@implementation BasicViewController
+@synthesize calendar = _calendar;
+
- (id)init
{
if ((self = [super init])) {
-
}
return self;
}
@@ -29,6 +30,8 @@ - (void)didReceiveMemoryWarning
- (void)loadView
{
self.view = [[UIView alloc] init];
+ self.calendar = [[iMonthlyView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
+ [self.view addSubview:self.calendar];
}
- (void)viewDidUnload
Oops, something went wrong.

0 comments on commit 2f2a858

Please sign in to comment.