先上效果图:
简单的日历分装
CJWCalendarItem.h
//
// CJWCalendarItem.h
// 日历
//
// Created by CJW on 16/7/21.
// Copyright © 2016年 cjw. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CJWCalendarItem : UIView
-(void)createCalendarViewWith:(NSDate *)date;
-(NSDate *)nextMonth:(NSDate *)date andNum:(NSInteger)num;
-(NSDate *)lastMonth:(NSDate *)date andNum:(NSInteger)num;
@property (nonatomic,strong) NSDate * date;
@property (nonatomic,copy) void (^calendarBlack)(NSInteger day,NSInteger month, NSInteger year);
@end
CJWCalendarItem.m
//
// CJWCalendarItem.m
// 日历
//
// Created by CJW on 16/7/21.
// Copyright © 2016年 cjw. All rights reserved.
//
#import "CJWCalendarItem.h"
@implementation CJWCalendarItem
{
UIButton * _selectButton;
NSMutableArray * _daysArray;
}
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_daysArray = [NSMutableArray arrayWithCapacity:42];
for (int i = 0; i< 42; i++) {
UIButton * button = [[UIButton alloc]init];
[self addSubview:button];
[_daysArray addObject:button];
}
}
return self;
}
#pragma mark - 日期
-(NSInteger)day:(NSDate *)date
{
NSDateComponents * components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
return [components day];
}
-(NSInteger)month:(NSDate *)date
{
NSDateComponents * components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
return [components month];
}
-(NSInteger)year:(NSDate *)date
{
NSDateComponents * components = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
return [components year];
}
-(NSInteger)firsetWeekdayInThisMonth:(NSDate *)date
{
NSCalendar * calendar = [NSCalendar currentCalendar];
[calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
NSDateComponents * comp = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
[comp setDay:1];
NSDate * firstDayOfMonthDate = [calendar dateFromComponents:comp];
NSUInteger firsetWeekDay = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
return firsetWeekDay - 1;
}
-(NSInteger)totaldaysInMonth:(NSDate* )date
{
NSRange daysInOfMonth = [[NSCalendar currentCalendar]rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
return daysInOfMonth.length;
}
-(NSDate *)lastMonth:(NSDate *)date andNum:(NSInteger)num
{
NSDateComponents * datecomponents = [[NSDateComponents alloc]init];
datecomponents.month =- num;
NSDate * newDate = [[NSCalendar currentCalendar]dateByAddingComponents:datecomponents toDate:date options:0];
return newDate;
}
-(NSDate *)nextMonth:(NSDate *)date andNum:(NSInteger)num
{
NSDateComponents * dateComponents = [[NSDateComponents alloc]init];
dateComponents.month += num;
NSDate * newDate = [[NSCalendar currentCalendar]dateByAddingComponents:dateComponents toDate:date options:0];
return newDate;
}
#pragma mark - 创建View
-(void)setDate:(NSDate * )date
{
_date = date;
[self createCalendarViewWith:date];
}
-(void)createCalendarViewWith:(NSDate *)date
{
CGFloat itemW = self.frame.size.width / 7;
CGFloat itemH = self.frame.size.height / 8 ;
//1 year month
UILabel * hearLable = [[UILabel alloc]init];
hearLable.text = [NSString stringWithFormat:@"%li年%li月",[self year:date],[self month:date]];
hearLable.font = [UIFont systemFontOfSize:14];
hearLable.textAlignment = NSTextAlignmentCenter;
hearLable.frame = CGRectMake(0, 0, self.frame.size.width, itemH);
[self addSubview:hearLable];
//2 weekday
NSArray * array = @[@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六"];
UIView * weekBg = [[UIView alloc]init];
weekBg.backgroundColor = [UIColor cyanColor];
weekBg.frame = CGRectMake(0, CGRectGetMaxY(hearLable.frame), self.frame.size.width, itemH);
[self addSubview:weekBg];
for (int i = 0; i< 7; i++) {
UILabel * week = [[UILabel alloc]init];
week.text = array[i];
week.backgroundColor = [UIColor clearColor];
week.textAlignment = NSTextAlignmentCenter;
week.textColor = [UIColor blackColor];
week.font = [UIFont systemFontOfSize:14];
week.frame = CGRectMake(itemW * i, 0, itemW, 32);
[weekBg addSubview:week];
}
for (int i = 0; i < 42; i ++) {
int x = (i%7) * itemW;
int y = (i/7) * itemH + CGRectGetMaxY(weekBg.frame);
UIButton * dayButtom = _daysArray[i];
dayButtom.frame = CGRectMake(x, y, itemW, itemH);
dayButtom.titleLabel.font = [UIFont systemFontOfSize:14];
dayButtom.titleLabel.textAlignment = NSTextAlignmentCenter;
dayButtom.layer.cornerRadius = 5.0;
[dayButtom setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[dayButtom addTarget:self action:@selector(logDate:) forControlEvents:UIControlEventTouchUpInside];
NSInteger dayINLastMonth = [self totaldaysInMonth:[self lastMonth:date andNum:1]];
NSInteger daysInthiMonth = [self totaldaysInMonth:date];
NSInteger firstWeekDay = [self firsetWeekdayInThisMonth:date];
NSInteger day = 0;
if (i < firstWeekDay )
{
day = dayINLastMonth - firstWeekDay + i + 1;
[self setStyle_BeyondThisMonth:dayButtom];
}
else if (i > firstWeekDay + dayINLastMonth -1)
{
day = i +1 - firstWeekDay - daysInthiMonth;
[self setStyle_BeyondThisMonth:dayButtom];
}
else
{
day = i - firstWeekDay +1;
[self setStyle_AfterToday:dayButtom];
}
[dayButtom setTitle:[NSString stringWithFormat:@"%li",day] forState:UIControlStateNormal];
if ([self month:date] == [self month:[NSDate date]]) {
NSInteger todayIndex = [self day:date] + firstWeekDay - 1;
if (i < todayIndex && i >= firstWeekDay) {
[self setStyle_BeforeToday:dayButtom];
}
else if (i == todayIndex)
{
[self setStyle_Today:dayButtom];
}
}
}
}
#pragma mark -
-(void)logDate:(UIButton *)dayBtn
{
_selectButton.selected = NO;
dayBtn.selected = YES;
_selectButton = dayBtn;
NSInteger day = [[dayBtn titleForState:UIControlStateNormal]integerValue];
NSDateComponents * comp = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay ) fromDate:self.date];
if (self.calendarBlack)
{
self.calendarBlack(day,[comp month],[comp year]);
}
}
#pragma mark - 日期按钮的状态;
- (void)setStyle_BeyondThisMonth:(UIButton *)btn
{
btn.enabled = NO;
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}
- (void)setStyle_BeforeToday:(UIButton *)btn
{
btn.enabled = NO;
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}
- (void)setStyle_Today:(UIButton *)btn
{
btn.enabled = YES;
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[btn setBackgroundColor:[UIColor orangeColor]];
}
- (void)setStyle_AfterToday:(UIButton *)btn
{
btn.enabled = YES;
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
@end
使用这个时间控件:
//
// ViewController.m
// 日历
//
// Created by CJW on 16/7/21.
// Copyright © 2016年 cjw. All rights reserved.
//
#import "ViewController.h"
#import "CJWCalendarItem.h"
#define CJWScreenW [UIScreen mainScreen].bounds.size.width
#define CJWScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@property (nonatomic,strong) CJWCalendarItem * myView_date;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myView_date =[[CJWCalendarItem alloc]init];
self.myView_date.frame = CGRectMake(50, 100, CJWScreenW-100, CJWScreenH/3);
self.myView_date.date = [NSDate date];
self.myView_date.calendarBlack = ^(NSInteger day,NSInteger month,NSInteger year)
{
};
[self.view addSubview:_myView_date];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end