iOS - 日历设置,支持同时多选设置不同类型的日期

前言:

由于公司是做的医疗服务这块的,所以提出了工作室预约,设置日期为,工作日,半休日,休息日三种,还要支持多选功能,下面提供下具体的做法,需要的小伙伴可以拿去哈
具体的效果如下:

Untitled01.gif
具体用法:

由于封装的方法比较多,我就先把使用方法写出来,改变下套路.

 //  为了防止有重影,最好是先移除
    [self.calendarView removeFromSuperview];
    self.calendarView = nil;
    
    NSDateFormatter *Mformatter = [[NSDateFormatter alloc] init];
    Mformatter.dateFormat = @"yyyy-MM";
    NSString *monthStr = [Mformatter stringFromDate:self.date];
    
    // 网络请求,设置工作室已经设置的日期
    NSMutableDictionary *listPrams = [NSMutableDictionary dictionary];
    listPrams[@"month"] = monthStr;
    listPrams[@"studioid"] = _studio_id;
    __weak typeof(self) weakself = self;
    [WSService requestBusiness:URL_BOOKING_WORKDAY_LISTBYMONTH params:listPrams sucCallBack:^(id result) {
        BOOL isValid = [APIRequestHandler isValidDataWithResult:result];
        if (isValid) {
            NSArray *data = result[@"data"];
            // 三种不同的日期类型
            NSMutableArray *restArray = [NSMutableArray array];
            NSMutableArray *mendArray = [NSMutableArray array];
            NSMutableArray *workArray = [NSMutableArray array];
            
            for (int i = 0; i < data.count; i ++) {
                NSDictionary *dict = data[i];
                NSString *caltype = dict[@"caltype"];
                NSString *calander = dict[@"calander"];
                calander = [calander substringWithRange:NSMakeRange(calander.length - 2,2)];
                if ([caltype isEqualToString:CALTYPE_RESTDAY]) {  // 休息日
                    [restArray addObject:calander];
                }else if ([caltype isEqualToString:CALTYPE_MENDDAY]) {  // 半休日
                    [mendArray addObject:calander];
                }else if ([caltype isEqualToString:CALTYPE_WORKDAY]) {  // 工作日
                    [workArray addObject:calander];
                }
            }
            
            weakself.calendarView = [[FyCalendarView alloc] initWithFrame:CGRectMake(0, 64, PDScreeenW, PDScreeenW)];
            
            weakself.calendarView.allDaysArr = workArray;
            weakself.calendarView.allDaysColor = workColor;
            
            weakself.calendarView.partDaysArr = mendArray;
            weakself.calendarView.partDaysColor = mendColor;
            
            weakself.calendarView.otherDaysArr = restArray;
            weakself.calendarView.otherDaysColor = restColor;
            
            weakself.calendarView.date = [NSDate date];
            weakself.calendarView.dateColor = selcetColor;
            
            [weakself.view addSubview:weakself.calendarView];
            
            // 默认选中当前日期
            UIButton *defaultButton = weakself.calendarView.defaultBtn;
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyy-MM-dd";
            NSString *dateStr = [formatter stringFromDate:weakself.date];
            if (defaultButton.selected) {
                [weakself.buttonsSelet addObject:defaultButton];
                [weakself.datesArray addObject:dateStr];
            }
            
            __weak typeof(weakself) weak_self = weakself;
            weakself.calendarView.calendarBlock =  ^(NSString *selectDate, UIButton *selectBtn){
                if (selectBtn.selected) {
                    [weak_self.buttonsSelet addObject:selectBtn];
                    [weak_self.datesArray addObject:selectDate];
                }else{
                    [weak_self.buttonsSelet removeObject:selectBtn];
                    [weak_self.datesArray removeObject:selectDate];
                }
                
            };
            // 设置下个月
            weakself.calendarView.nextMonthBlock = ^(){
                [weak_self setupNextMonth];
                
            };
            // last month
            weakself.calendarView.lastMonthBlock = ^(){
                [weak_self setupLastMonth];
            };
            
        }
    } failCallBack:^(id error) {
        
    } hasHud:NO];

接下来的这两个方法自己实现下,一样的道理

[weak_self setupNextMonth];
[weak_self setupLastMonth];
具体实现代码:
  • h 文件
#import <UIKit/UIKit.h>

@protocol CalendarView <NSObject>
/**
 *  点击返回的日期
 */
- (void)setupToday:(NSInteger)day Month:(NSInteger)month Year:(NSInteger)year;
@end

@interface CalendarView : UIView

//set 选择日期
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) UIColor *dateColor;
//年月label
@property (nonatomic, strong) UILabel *headlabel;
// 选中文字颜色
@property (nonatomic, strong) UIColor *headColor;
//weekView
@property (nonatomic, strong) UIView *weekBg;
@property (nonatomic, strong) UIColor *weekDaysColor;

// 全天可用
@property (nonatomic, strong) NSMutableArray *allDaysArr;
@property (nonatomic, strong) UIColor *allDaysColor;

// 部分时段可用
@property (nonatomic, strong) NSMutableArray *partDaysArr;
@property (nonatomic, strong) UIColor *partDaysColor;

// 外加部分选择
@property (nonatomic, strong) NSMutableArray *otherDaysArr;
@property (nonatomic, strong) UIColor *otherDaysColor;

// 是否只显示本月日期,默认->NO
@property (nonatomic, assign) BOOL isShowOnlyMonthDays;

// 默认当前日期的按钮
@property (weak, nonatomic) UIButton *defaultBtn;

//创建日历
- (void)createCalendarViewWith:(NSDate *)date;
/**
 *  nextMonth
 *
 *  @param date nil = 当前日期的下一个月
 */
//- (void)setNextMonth:(NSDate *)date;
- (NSDate *)nextMonth:(NSDate *)date;

/**
 *  lastMonth
 *
 *  @param date nil -> 当前日期的上一个月
 */
//- (void)setLastMonth:(NSDate *)date;
- (NSDate *)lastMonth:(NSDate *)date;

/**
 *  nextMonth and lastMonth
 */
@property (nonatomic, copy) void(^nextMonthBlock)();
@property (nonatomic, copy) void(^lastMonthBlock)();
/**
 *  点击返回日期
 */
@property (nonatomic, copy) void(^calendarBlock)(NSString *seletDate, UIButton *selectBtn);
  • m 文件
#import "CalendarView.h"
#import "UIColor+Hex.h"

@interface CalendarView ()
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSMutableArray *daysArray;

@end

@implementation CalendarView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDate];
        [self setupNextAndLastMonthView];
    }
    return self;
}

- (void)setupDate {
    self.daysArray = [NSMutableArray arrayWithCapacity:42];
    for (int i = 0; i < 42; i++) {
        UIButton *button = [[UIButton alloc] init];
        [self addSubview:button];
        [_daysArray addObject:button];
        [button addTarget:self action:@selector(logDate:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)setupNextAndLastMonthView {
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"to_left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(nextAndLastMonth:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    leftBtn.tag = 1;
    leftBtn.frame = CGRectMake(0, 0, 40, 40);
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"to_right"] forState:UIControlStateNormal];
    rightBtn.tag = 2;
    [rightBtn addTarget:self action:@selector(nextAndLastMonth:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    rightBtn.frame = CGRectMake(self.frame.size.width - 40, 0, 40, 40);
}

- (void)nextAndLastMonth:(UIButton *)button {
    if (button.tag == 1) {
        if (self.lastMonthBlock) {
            self.lastMonthBlock();
        }
    } else {
        if (self.nextMonthBlock) {
            self.nextMonthBlock();
        }
    }
}

#pragma mark - create 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.width / 7;
    
    // 1.year month
    if (self.headlabel == nil) {
        self.headlabel = [[UILabel alloc] init];
    }
    self.headlabel.text     = [NSString stringWithFormat:@"%li年%li月",(long)[self year:date],(long)[self month:date]];
    NSLog(@"%@", self.headlabel.text);
    self.headlabel.font     = [UIFont systemFontOfSize:14];
    self.headlabel.frame           = CGRectMake(0, 0, self.frame.size.width, itemH * 0.8);
    self.headlabel.textAlignment   = NSTextAlignmentCenter;
    self.headlabel.textColor = self.headColor;
    [self addSubview: self.headlabel];
    
    // 2.weekday
    NSArray *array = @[@"日", @"一", @"二", @"三", @"四", @"五", @"六"];
    self.weekBg = [[UIView alloc] init];
    //    weekBg.backgroundColor = [UIColor orangeColor];
    self.weekBg.frame = CGRectMake(0, CGRectGetMaxY(self.headlabel.frame), self.frame.size.width, itemH * 0.6);
    [self addSubview:self.weekBg];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, PDScreeenW, 1.0)];
    line1.backgroundColor = LineCommanColorA;
    [self.weekBg addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.weekBg.frame.size.height - 1.0, PDScreeenW, 1.0)];
    line2.backgroundColor = LineCommanColorA;
    [self.weekBg addSubview:line2];
    
    for (int i = 0; i < 7; i++) {
        UILabel *week = [[UILabel alloc] init];
        week.text     = array[i];
        week.font     = [UIFont systemFontOfSize:14];
        week.frame    = CGRectMake(itemW * i, (itemH * 0.6 - 20) * 0.5, itemW, 20);
        week.textAlignment   = NSTextAlignmentCenter;
        week.backgroundColor = [UIColor clearColor];
        week.textColor       = self.weekDaysColor;
        [self.weekBg addSubview:week];
    }
    
    //  3.days (1-31)
    for (int i = 0; i < 42; i++) {
        
        int x = (i % 7) * itemW ;
        int y = (i / 7) * itemH + CGRectGetMaxY(self.weekBg.frame) + 5;
        
        UIButton *dayButton = _daysArray[i];
        dayButton.selected = NO;
        dayButton.frame = CGRectMake(x, y, itemW-6, itemH-6);
        
        dayButton.layer.cornerRadius = dayButton.frame.size.width / 2;
        dayButton.layer.masksToBounds = YES;
        dayButton.layer.borderWidth = 3.0 ;
        dayButton.layer.borderColor = [[UIColor clearColor] CGColor];
        
        dayButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
        dayButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [dayButton addTarget:self action:@selector(logDate:) forControlEvents:UIControlEventTouchUpInside];
        
        NSInteger daysInLastMonth = [self totaldaysInMonth:[self lastMonth:date]];
        NSInteger daysInThisMonth = [self totaldaysInMonth:date];
        NSInteger firstWeekday    = [self firstWeekdayInThisMonth:date];
        
        NSInteger day = 0;
        
        
        if (i < firstWeekday) {
            day = daysInLastMonth - firstWeekday + i + 1;
            [self setStyle_BeyondThisMonth:dayButton];
            
        }else if (i > firstWeekday + daysInThisMonth - 1){
            day = i + 1 - firstWeekday - daysInThisMonth;
            [self setStyle_BeyondThisMonth:dayButton];
            
        }else{
            day = i - firstWeekday + 1;
            [dayButton setTitle:[NSString stringWithFormat:@"%li", (long)day] forState:UIControlStateNormal];
            [self setStyle_AfterToday:dayButton];
        }
        
        [dayButton setTitle:[NSString stringWithFormat:@"%li", (long)day] forState:UIControlStateNormal];
        dayButton.titleLabel.font = [UIFont systemFontOfSize:21];
        
        // this month
        if ([self month:date] == [self month:[NSDate date]]) {
            
            NSInteger todayIndex = [self day:date] + firstWeekday - 1;
            
            if (i < todayIndex && i >= firstWeekday) {
                //                [self setStyle_BeforeToday:dayButton];
            }else if(i ==  todayIndex){
                [self setStyle_Today:dayButton];
            }
            
        }
    }
}

#pragma mark - output date
-(void)logDate:(UIButton *)dayBtn
{
    
    dayBtn.selected = !dayBtn.selected;
    if (dayBtn.selected) {
        dayBtn.layer.borderColor = [self.dateColor CGColor];
    }else{

        dayBtn.layer.borderColor = [[UIColor clearColor] CGColor];
    }
    NSInteger day = [[dayBtn titleForState:UIControlStateNormal] integerValue];
    NSString *dayStr;
    if (day < 10) {
        dayStr = [NSString stringWithFormat:@"0%zd",day];
    }else{
        dayStr = [NSString stringWithFormat:@"%zd",day];
    }
    NSDateComponents *comp = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:self.date];
    NSInteger month = [comp month];
    NSString *monthStr;
    if (month < 10) {
        monthStr = [NSString stringWithFormat:@"0%li",month];
    }else{
        monthStr = [NSString stringWithFormat:@"%li",month];
    }
    NSString *dateStr = [NSString stringWithFormat:@"%li-%@-%@", [comp year],monthStr, dayStr];
    
    if (self.calendarBlock) {
        self.calendarBlock(dateStr, dayBtn);
    }
    
}

#pragma mark - date button style

- (void)setStyle_BeyondThisMonth:(UIButton *)btn
{
    btn.enabled = NO;
    if (self.isShowOnlyMonthDays) {
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    } else {
        [btn setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        
    }
}

- (void)setStyle_Today:(UIButton *)btn
{
    btn.selected = YES;
    self.defaultBtn = btn;
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:CommanColor forState:UIControlStateSelected];
    btn.layer.borderColor = [self.dateColor CGColor];
}

- (void)setStyle_AfterToday:(UIButton *)btn
{
    [btn setTitleColor:CommanColor forState:UIControlStateSelected];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    for (NSString *str in self.allDaysArr) {
        NSString *btnTitle = btn.titleLabel.text;
        NSInteger btnInt = [btnTitle integerValue];
        if (btnInt < 10)  btnTitle = [NSString stringWithFormat:@"0%@",btnTitle];
        if ([str isEqualToString:btnTitle]) {
            btn.backgroundColor = self.allDaysColor;
    
        }
    }
    for (NSString *str in self.otherDaysArr) {
        NSString *btnTitle = btn.titleLabel.text;
        NSInteger btnInt = [btnTitle integerValue];
        if (btnInt < 10) {
            btnTitle = [NSString stringWithFormat:@"0%@",btnTitle];
        }
        if ([str isEqualToString:btnTitle]) {
            btn.backgroundColor = self.otherDaysColor;
        }
    }
    for (NSString *str in self.partDaysArr) {
        NSString *btnTitle = btn.titleLabel.text;
        NSInteger btnInt = [btnTitle integerValue];
        if (btnInt < 10) {
            btnTitle = [NSString stringWithFormat:@"0%@",btnTitle];
        }
        if ([str isEqualToString:btnTitle]) {
            btn.backgroundColor = self.partDaysColor;
        }
    }
}

#pragma mark - Lazy loading
- (NSMutableArray *)allDaysArr {
    if (!_allDaysArr) {
        _allDaysArr = [NSMutableArray array];
    }
    return _allDaysArr;
}
- (NSMutableArray *)otherDaysArr{
    if (!_otherDaysArr) {
        _otherDaysArr = [NSMutableArray array];
    }
    return _otherDaysArr;
}
- (NSMutableArray *)partDaysArr {
    if (!_partDaysArr) {
        _partDaysArr = [NSMutableArray array];
    }
    return _partDaysArr;
}

- (UIColor *)headColor {
    if (!_headColor) {
        _headColor = [UIColor blackColor];
    }
    return _headColor;
}

- (UIColor *)dateColor {
    if (!_dateColor) {
        _dateColor = [UIColor colorWithHexString:@"#7448B4" alpha:0.5];
    }
    return _dateColor;
}
- (UIColor *)weekDaysColor {
    if (!_weekDaysColor) {
        _weekDaysColor = [UIColor lightGrayColor];
    }
    return _weekDaysColor;
}

//一个月第一个周末
- (NSInteger)firstWeekdayInThisMonth:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];//1.Sun. 2.Mon. 3.Thes. 4.Wed. 5.Thur. 6.Fri. 7.Sat.
    NSDateComponents *component = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    [component setDay:1];
    NSDate *firstDayOfMonthDate = [calendar dateFromComponents:component];
    NSUInteger firstWeekDay = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:firstDayOfMonthDate];
    return firstWeekDay - 1;
}

//总天数
- (NSInteger)totaldaysInMonth:(NSDate *)date{
    NSRange daysInOfMonth = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return daysInOfMonth.length;
}

#pragma mark - month +/-

- (NSDate *)lastMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

- (NSDate*)nextMonth:(NSDate *)date{
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = +1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:0];
    return newDate;
}

#pragma mark - date get: day-month-year

- (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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容