iOS开发 FSCalendar的使用

2018-05-09  本文已影响146人  lczalh
还款日.png
日历.png
选择还款日.png

这里是根据还款日是几号来显示可选的日期
1.还款日是当天则不能选择还款日
2.还款日是否大于或小于当天
大于:则显示当天到还款日前一天
小于:则显示当天到下一个月的还款日前一天

遵守协议

<FSCalendarDataSource,FSCalendarDelegate,FSCalendarDelegateAppearance>

创建日历

self.calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(5, 40, kWidth-10, kHeight/2-30)];
        self.calendar.dataSource = self;
        self.calendar.delegate = self;
        self.calendar.pagingEnabled = NO; // important
        //多选
        self.calendar.allowsMultipleSelection = YES;
        //今天背景颜色
        self.calendar.appearance.todayColor = [UIColor clearColor];
        //今天标题颜色
        self.calendar.appearance.titleTodayColor = [UIColor blackColor];
        self.calendar.placeholderType = FSCalendarPlaceholderTypeFillSixRows;

如何创建NSDate对象

self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.dateFormat = @"yyyy-MM-dd";
NSDate *date = [self.formatter dateFromString:@"2018-05-9"];

如何使用NSCalendar处理NSDate

self.gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];     

协议方法

//选中某一天进行相关操作
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition
{
    [self.dateAry addObject:date];
    [calendar reloadData];
    NSInteger day = [self.gregorian component:NSCalendarUnitDay fromDate:date];
    NSInteger year = [self.gregorian component:NSCalendarUnitYear fromDate:date];
    NSInteger month = [self.gregorian component:NSCalendarUnitMonth fromDate:date];
    [self.monthAndDayAry addObject:[NSString stringWithFormat:@"%02ld%02ld",month,day]];
    NSLog(@"did select %@,%02ld,%02ld,%2ld,%@",[self.dateFormatter stringFromDate:date],day,month,year,self.monthAndDayAry);
}

//取消选中的日期进行相关操作
- (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date {
    [self.dateAry removeObject:date];
    [calendar reloadData];
    NSInteger day = [self.gregorian component:NSCalendarUnitDay fromDate:date];
    NSInteger year = [self.gregorian component:NSCalendarUnitYear fromDate:date];
    NSInteger month = [self.gregorian component:NSCalendarUnitMonth fromDate:date];
    [self.monthAndDayAry removeObject:[NSString stringWithFormat:@"%02ld%02ld",month,day]];
    NSLog(@"did select %@,%ld,%ld,%ld,%@",[self.dateFormatter stringFromDate:date],day,month,year,self.monthAndDayAry);
}

//设置副标题
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
    for (NSDate *d in self.dateAry) {
        if ([self.gregorian isDate:d inSameDayAsDate:date]) {
            return @"还款";
        }
    }
    return nil;
}

//设置可选日期
- (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date {
    NSInteger year;
    NSInteger month;
    //获取下个月日期
    NSDate *nextMonth;
    NSInteger day = [self.gregorian component:NSCalendarUnitDay fromDate:[self.calendar today]];
    year = [self.gregorian component:NSCalendarUnitYear fromDate:[self.calendar today]];
    month = [self.gregorian component:NSCalendarUnitMonth fromDate:[self.calendar today]];
        //判断今天日期 和 当前选择日期。是大于还是等于还是小于
        switch ([self.gregorian compareDate:[self.calendar today] toDate:date toUnitGranularity:NSCalendarUnitDay|NSCalendarUnitMonth]) {
            case NSOrderedAscending://大于
                if ([self.payDay integerValue] < day) {
                    //获取下个月日期
                    nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:[self.calendar today] options:0];
                    month = [self.gregorian component:NSCalendarUnitMonth fromDate:nextMonth];
                    //获取还款日
                    NSDate *newDate = [self.dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%@",(long)year,(long)month,self.payDay]];
                    //判断当前日期 到还款日
                    switch ([self.gregorian compareDate:date toDate:newDate toUnitGranularity:NSCalendarUnitDay]) {
                            
                        case NSOrderedAscending:
                            return YES;
                        case NSOrderedSame:
                            
                            return NO;
                        case NSOrderedDescending:
                            return NO;
                    }
                }else{
                   
                    NSDate *newDate = [self.dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%@",(long)year,(long)month,self.payDay]];
                
                    switch ([self.gregorian compareDate:newDate toDate:date toUnitGranularity:NSCalendarUnitDay]) {
                        case NSOrderedAscending://大于
                            return NO;
                        case NSOrderedSame://等于
                            
                            return NO;
                        
                        case NSOrderedDescending://小于
                            
                            return YES;
                    }
                }
                
            case NSOrderedSame://等于
                if ([self.payDay integerValue] == day) {
                    return NO;
                }else{
                    return YES;
                }
                
            case NSOrderedDescending://小于
            
                return NO;
                
        }
   
}

//设置当前月与非当前月字体颜色
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance titleDefaultColorForDate:(NSDate *)date {
    NSLog(@"%@",self.payDay);
    NSInteger year;
    NSInteger month;
    //获取下个月日期
    NSDate *nextMonth;
    NSInteger day = [self.gregorian component:NSCalendarUnitDay fromDate:[self.calendar today]];
    year = [self.gregorian component:NSCalendarUnitYear fromDate:[self.calendar today]];
    month = [self.gregorian component:NSCalendarUnitMonth fromDate:[self.calendar today]];
    
    //判断今天日期 和 每月日期。是大于还是等于还是小于
    switch ([self.gregorian compareDate:[self.calendar today] toDate:date toUnitGranularity:NSCalendarUnitDay|NSCalendarUnitMonth]) {
        case NSOrderedAscending://大于
            
            if ([self.payDay integerValue] < day) {
                //获取下个月日期
                nextMonth = [self.gregorian dateByAddingUnit:NSCalendarUnitMonth value:1 toDate:[self.calendar today] options:0];
                month = [self.gregorian component:NSCalendarUnitMonth fromDate:nextMonth];
                //获取还款日
                NSDate *newDate = [self.dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%@",(long)year,(long)month,self.payDay]];
                //判断当前日期 到还款日
                switch ([self.gregorian compareDate:date toDate:newDate toUnitGranularity:NSCalendarUnitDay]) {
                        
                    case NSOrderedAscending:
                        return [UIColor blackColor];
                    case NSOrderedSame:
                        
                        return RGBColor(153, 153, 153, 1);
                    case NSOrderedDescending:
                        return RGBColor(153, 153, 153, 1);
                }
   
            }else{
        
                NSDate *newDate = [self.dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-%ld-%@",(long)year,(long)month,self.payDay]];
                //判断新日期
                switch ([self.gregorian compareDate:newDate toDate:date toUnitGranularity:NSCalendarUnitDay]) {
                    case NSOrderedAscending://大于
                        return RGBColor(153, 153, 153, 1);
                    case NSOrderedSame://等于
                        return RGBColor(153, 153, 153, 1);
                        
                    case NSOrderedDescending://小于
                        return [UIColor blackColor];
                }
            }
            
        case NSOrderedSame://等于
            if ([self.payDay integerValue] == day) {
                return RGBColor(153, 153, 153, 1);
            }else{
                return [UIColor blackColor];
            }
            
        case NSOrderedDescending://小于
            return RGBColor(153, 153, 153, 1);
            
    }
}
上一篇下一篇

猜你喜欢

热点阅读