Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions 21651191俞晨昊/project02/Calendar/Calendar/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// AppDelegate.h
// Calendar
//
// Created by ych.
// Copyright © 2016年 mutouren. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

55 changes: 55 additions & 0 deletions 21651191俞晨昊/project02/Calendar/Calendar/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// AppDelegate.m
// Created by ych.
//

#import "AppDelegate.h"
#import "CalendarViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
CalendarViewController *rootViewController = [[CalendarViewController alloc] init];
rootViewController.view.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = rootViewController;

[self.window makeKeyAndVisible];
return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
17 changes: 17 additions & 0 deletions 21651191俞晨昊/project02/Calendar/Calendar/CalendarModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// CalendarModel.h
// Calendar
// Created by ych.
//

#import <Foundation/Foundation.h>

@interface CalendarModel : NSObject

@property (nonatomic)NSInteger month;
@property (nonatomic)NSInteger year;

- (void)setYear:(NSInteger)year andMonth:(NSInteger)month;
- (NSArray *)getMonthModel;

@end
86 changes: 86 additions & 0 deletions 21651191俞晨昊/project02/Calendar/Calendar/CalendarModel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// CalendarModel.m
// Calendar
// Created by ych.

#import "CalendarModel.h"

@interface CalendarModel ()

@end

@implementation CalendarModel
{
NSArray *monthModel;
}

- (instancetype)init
{
self = [super init];
if (self) {
[self setYear:[self currentYear] andMonth:[self currentMonth]];
}
return self;
}

- (void)setYear:(NSInteger)year andMonth:(NSInteger)month
{
self.month = month;
self.year = year;
[self calculateMonthModel];
}
//获取月份模型,以6 * 7数组存储
- (void)calculateMonthModel
{
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
[components setMonth:self.month];
[components setYear:self.year];
NSDate *date = [calendar dateFromComponents:components];
components = [calendar components:NSCalendarUnitWeekday fromDate:date];
NSInteger weekday = components.weekday;
NSInteger daysNumber = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithCapacity:42];
for (NSInteger i = 0; i < weekday - 1; i++)
{
[mutableArray addObject:@0];
}
for (NSInteger i = 1; i < daysNumber + 1; i++)
{
[mutableArray addObject:[NSNumber numberWithInteger:i]];
}
for (NSInteger i = 0; i < 42 - daysNumber - weekday + 1; i++)
{
[mutableArray addObject:@0];
}
monthModel = [NSArray arrayWithArray:mutableArray];
}

-(NSArray *)getMonthModel
{
return monthModel;
}

- (NSInteger)currentYear
{
NSDate *date =[NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
components = [calendar components:NSCalendarUnitYear fromDate:date];
//NSLog(@"components.year: %ld", components.year);
return components.year;
}

- (NSInteger)currentMonth
{
NSDate *date =[NSDate date];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
components = [calendar components:NSCalendarUnitMonth fromDate:date];
//NSLog(@"components.month: %ld", components.month);
return components.month;
}


@end
16 changes: 16 additions & 0 deletions 21651191俞晨昊/project02/Calendar/Calendar/CalendarView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CalendarView.h
// Created by ych.
// Copyright © 2016年 mutouren. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CalendarModel.h"

@interface CalendarView : UIView

@property (nonatomic)CalendarModel *calendarModel;

- (CGSize)minBoundingSize;

@end
94 changes: 94 additions & 0 deletions 21651191俞晨昊/project02/Calendar/Calendar/CalendarView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// CalendarView.m
// Created by ych.
// Copyright © 2016年 mutouren. All rights reserved.


#import "CalendarView.h"

@interface CalendarView ()

@property (nonatomic)NSArray *monthStringArray;
@property (nonatomic)NSArray *monthModel;
//@property (nonatomic)NSString *stringToDraw;

@end

@implementation CalendarView

- (instancetype)init
{
return [self initWithCalendarModel:[[CalendarModel alloc] init]];
}

- (instancetype)initWithCalendarModel: (CalendarModel *)calendarModel
{
self = [super init];
if (self != nil)
{
self.calendarModel = calendarModel;
}

return self;
}

- (void)drawRect:(CGRect)rect
{
//[super drawRect:rect];
// CGSize size = [_stringToDraw sizeWithAttributes:nil];
// CGRect newRect = rect;
// newRect.size = size;
[super drawRect:rect];
NSString *string = [self transformCalendarModelIntoString];

[string drawAtPoint:CGPointMake(0, 0) withAttributes:nil];
}

- (NSString *)transformCalendarModelIntoString
{
//NSLog(@"[self.calendarModel.month - 1]: %ld", self.calendarModel.month - 1);
NSMutableString *string = [[NSMutableString alloc] init];
//NSMutableString *string = [NSMutableString stringWithFormat:@" %@ %ld\n", self.monthStringArray[self.calendarModel.month - 1], (long)self.calendarModel.year];
[string appendString:@"Sun Mon Tue Wed Thu Fri Sat\n\n"];
//[string appendString:@"日 一 二 三 四 五 六\n"];
for (int i = 0; i < 42; i++)
{
NSInteger value = [((NSNumber *)self.monthModel[i]) integerValue];
if (value == 0) [string appendString:@" "];
else if (value < 10) [string appendFormat:@" %ld ", value];
else [string appendFormat:@" %ld ", value];
//NSLog(@"\n%2d ", 6);
if (i % 7 == 6) [string appendString:@"\n\n"];
}
//NSLog(@"%@", string);
return [NSString stringWithString:string];

}

- (CGSize)minBoundingSize
{
NSString *string = [self transformCalendarModelIntoString];
return [string sizeWithAttributes:nil];
}

#pragma mark - Accessor Methods

- (NSArray *)monthStringArray
{
if (_monthStringArray == nil)
{
_monthStringArray = @[@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"];
//_monthStringArray = @[@"一月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月", @"九月", @"十月", @"十一月", @"十二月"];
}
return _monthStringArray;
}

- (void)setCalendarModel:(CalendarModel *)calendarModel
{
_calendarModel = calendarModel;
self.monthModel = [calendarModel getMonthModel];
}



@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// CalendarViewController.h
// Created by ych.
// Copyright © 2016年 mutouren. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CalendarViewController : UIViewController

@end
Loading