// // MultipleSelectionViewController.m // FSCalendar // // Created by dingwenchao on 9/9/15. // Copyright (c) 2015 wenchaoios. All rights reserved. // #import "MultipleSelectionViewController.h" @interface MultipleSelectionViewController () @property (strong, nonatomic) NSDateFormatter *dateFormatter; @property (strong, nonatomic) NSCalendar *gregorian; @end @implementation MultipleSelectionViewController - (instancetype)init { self = [super init]; if (self) { self.title = @"FSCalendar"; } return self; } - (void)loadView { UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; view.backgroundColor = [UIColor groupTableViewBackgroundColor]; self.view = view; CGFloat height = [[UIDevice currentDevice].model hasPrefix:@"iPad"] ? 450 : 300; FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.navigationController.navigationBar.frame), view.bounds.size.width, height)]; calendar.dataSource = self; calendar.delegate = self; calendar.backgroundColor = [UIColor whiteColor]; calendar.allowsMultipleSelection = YES; [self.view addSubview:calendar]; self.calendar = calendar; } - (void)viewDidLoad { [super viewDidLoad]; self.gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; self.dateFormatter = [[NSDateFormatter alloc] init]; self.dateFormatter.dateFormat = @"yyyy/MM/dd"; NSDate *tomorrow = [self.gregorian dateByAddingUnit:NSCalendarUnitDay value:1 toDate:[NSDate date] options:0]; [_calendar selectDate:tomorrow]; } - (BOOL)calendar:(FSCalendar *)calendar shouldSelectDate:(NSDate *)date { BOOL shouldDedeselect = [self.gregorian component:NSCalendarUnitDay fromDate:date] != 5; if (!shouldDedeselect) { [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Forbidden date %@ to be selected",[self.dateFormatter stringFromDate:date]] message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; return NO; } return YES; } - (BOOL)calendar:(FSCalendar *)calendar shouldDeselectDate:(NSDate *)date { BOOL shouldDedeselect = [self.gregorian component:NSCalendarUnitDay fromDate:date] != 7; if (!shouldDedeselect) { [[[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Forbidden date %@ to be deselected",[self.dateFormatter stringFromDate:date]] message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; return NO; } return YES; } - (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition { NSMutableArray *selectedDates = [NSMutableArray arrayWithCapacity:calendar.selectedDates.count]; [calendar.selectedDates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [selectedDates addObject:[self.dateFormatter stringFromDate:obj]]; }]; NSLog(@"selected dates is %@",selectedDates); } - (void)calendar:(FSCalendar *)calendar didDeselectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition { NSMutableArray *selectedDates = [NSMutableArray arrayWithCapacity:calendar.selectedDates.count]; [calendar.selectedDates enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [selectedDates addObject:[self.dateFormatter stringFromDate:obj]]; }]; NSLog(@"selected dates is %@",selectedDates); } - (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance selectionColorForDate:(NSDate *)date { if ([self.gregorian component:NSCalendarUnitDay fromDate:date] % 2 == 0) { return appearance.selectionColor; } return [UIColor purpleColor]; } - (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderDefaultColorForDate:(NSDate *)date { if ([@[@17,@18,@19] containsObject:@([self.gregorian component:NSCalendarUnitDay fromDate:date])]) { return [UIColor magentaColor]; } return appearance.borderDefaultColor; } @end