diff --git a/CalendarDemo/MonthViewController.m b/CalendarDemo/MonthViewController.m index bc0074cd..9232eee7 100755 --- a/CalendarDemo/MonthViewController.m +++ b/CalendarDemo/MonthViewController.m @@ -19,6 +19,8 @@ - (void)viewDidLoad { [super viewDidLoad]; //self.monthPlannerView.dateFormat = @"dd MMM\nYYYY"; + + self.monthPlannerView.allowCustomDayBackgroundColor = YES; } #pragma mark - MGCMonthPlannerViewController @@ -75,6 +77,23 @@ - (NSAttributedString*)monthPlannerView:(MGCMonthPlannerView *)view attributedSt return attrStr; } +- (UIColor*)monthPlannerView:(MGCMonthPlannerView*)view colorForDayCellAtDate:(NSDate*)date +{ + NSDateComponents *component = [self.calendar components:NSCalendarUnitWeekday fromDate:date]; + NSInteger weekday = [component weekday]; + + if ([self.calendar isDateInWeekend:date]) { + return view.weekendDayBackgroundColor; + } else { + if (weekday % 2 == 0) { + return [UIColor grayColor]; + } else { + return [UIColor whiteColor]; + } + } +} + + #pragma mark - CalendarViewControllerNavigation - (NSDate*)centerDate diff --git a/CalendarLib/MGCDayPlannerEKViewController.m b/CalendarLib/MGCDayPlannerEKViewController.m index 224bd6c5..73e03b22 100755 --- a/CalendarLib/MGCDayPlannerEKViewController.m +++ b/CalendarLib/MGCDayPlannerEKViewController.m @@ -366,7 +366,8 @@ - (MGCEventView*)dayPlannerView:(MGCDayPlannerView*)view viewForEventOfType:(MGC evCell.font = [UIFont systemFontOfSize:11]; evCell.title = ev.title; evCell.subtitle = ev.location; - evCell.color = [UIColor colorWithCGColor:ev.calendar.CGColor]; + evCell.eventNormalBackgroundColor = [UIColor colorWithCGColor:ev.calendar.CGColor]; + evCell.eventSelectedBackgroundColor = [UIColor colorWithCGColor:ev.calendar.CGColor]; evCell.style = MGCStandardEventViewStylePlain|MGCStandardEventViewStyleSubtitle; evCell.style |= (type == MGCAllDayEventType) ?: MGCStandardEventViewStyleBorder; return evCell; @@ -424,7 +425,8 @@ - (MGCEventView*)dayPlannerView:(MGCDayPlannerView*)view viewForNewEventOfType:( MGCStandardEventView *evCell = [MGCStandardEventView new]; evCell.title = NSLocalizedString(@"New Event", nil); - evCell.color = [UIColor colorWithCGColor:defaultCalendar.CGColor]; + evCell.eventNormalBackgroundColor = [UIColor colorWithCGColor:defaultCalendar.CGColor]; + evCell.eventSelectedBackgroundColor = [UIColor colorWithCGColor:defaultCalendar.CGColor]; return evCell; } diff --git a/CalendarLib/MGCMonthPlannerEKViewController.m b/CalendarLib/MGCMonthPlannerEKViewController.m index 9935522e..e13ba58c 100755 --- a/CalendarLib/MGCMonthPlannerEKViewController.m +++ b/CalendarLib/MGCMonthPlannerEKViewController.m @@ -390,7 +390,8 @@ - (MGCEventView*)monthPlannerView:(MGCMonthPlannerView*)view cellForEventAtIndex evCell.title = ev.title; evCell.subtitle = ev.location; evCell.detail = [self.dateFormatter stringFromDate:ev.startDate]; - evCell.color = [UIColor colorWithCGColor:ev.calendar.CGColor]; + evCell.eventNormalBackgroundColor = [UIColor colorWithCGColor:ev.calendar.CGColor]; + evCell.eventSelectedBackgroundColor = [UIColor colorWithCGColor:ev.calendar.CGColor]; NSDate *start = [self.calendar mgc_startOfDayForDate:ev.startDate]; NSDate *end = [self.calendar mgc_nextStartOfDayForDate:ev.endDate]; @@ -416,7 +417,8 @@ - (MGCEventView*)monthPlannerView:(MGCMonthPlannerView*)view cellForNewEventAtDa MGCStandardEventView *evCell = [MGCStandardEventView new]; evCell.title = NSLocalizedString(@"New Event", nil); - evCell.color = [UIColor colorWithCGColor:defaultCalendar.CGColor]; + evCell.eventNormalBackgroundColor = [UIColor colorWithCGColor:defaultCalendar.CGColor]; + evCell.eventSelectedBackgroundColor = [UIColor colorWithCGColor:defaultCalendar.CGColor]; return evCell; } diff --git a/CalendarLib/MGCMonthPlannerView.h b/CalendarLib/MGCMonthPlannerView.h index febd05da..57ff7d6c 100755 --- a/CalendarLib/MGCMonthPlannerView.h +++ b/CalendarLib/MGCMonthPlannerView.h @@ -221,6 +221,12 @@ typedef NS_ENUM(NSUInteger, MGCMonthPlannerScrollAlignment) { */ @property (nonatomic) BOOL canMoveEvents; +/*! + @abstract Determines whether an day can have different background color. + @discussion The default value is NO. + */ +@property (nonatomic) BOOL allowCustomDayBackgroundColor; + /*! @abstract The object that provides the data for the month planner view @discussion The data source must adopt the `MGCMonthPlannerViewDataSource` protocol. @@ -463,6 +469,14 @@ typedef NS_ENUM(NSUInteger, MGCMonthPlannerScrollAlignment) { */ - (BOOL)monthPlannerView:(MGCMonthPlannerView*)view canMoveCellForEventAtIndex:(NSUInteger)index date:(NSDate*)date; +/*! + @abstract Asks the data source for the color that corresponds to the specified date in the month planner view. + @discussion This method is not called when allowCustomDayBackgroundColor property is set to NO. + + */ +- (UIColor*)monthPlannerView:(MGCMonthPlannerView*)view colorForDayCellAtDate:(NSDate*)date; + + @end /*! diff --git a/CalendarLib/MGCMonthPlannerView.m b/CalendarLib/MGCMonthPlannerView.m index 5e3dff4a..162bb0f9 100755 --- a/CalendarLib/MGCMonthPlannerView.m +++ b/CalendarLib/MGCMonthPlannerView.m @@ -123,6 +123,7 @@ - (void)setup _selectedEventDate = nil; _canCreateEvents = YES; _canMoveEvents = YES; + _allowCustomDayBackgroundColor = NO; _calendarBackgroundColor = [UIColor whiteColor]; _weekDayBackgroundColor = [UIColor whiteColor]; _weekendDayBackgroundColor = [UIColor colorWithWhite:.97 alpha:.8]; @@ -1399,6 +1400,10 @@ - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellFo cell.dayLabel.attributedText = attrStr; cell.backgroundColor = [self.calendar isDateInWeekend:date] ? self.weekendDayBackgroundColor : self.weekDayBackgroundColor; + if ([self.dataSource respondsToSelector:@selector(monthPlannerView:colorForDayCellAtDate:)] && self.allowCustomDayBackgroundColor) { + cell.backgroundColor = [self.dataSource monthPlannerView:self colorForDayCellAtDate:date]; + } + if (self.style & MGCMonthPlannerStyleDots) { NSUInteger eventsCounts = [self.dataSource monthPlannerView:self numberOfEventsAtDate:date]; cell.showsDot = eventsCounts > 0; diff --git a/CalendarLib/MGCStandardEventView.h b/CalendarLib/MGCStandardEventView.h index 21505b4d..10a0ac2b 100755 --- a/CalendarLib/MGCStandardEventView.h +++ b/CalendarLib/MGCStandardEventView.h @@ -58,8 +58,14 @@ typedef NS_OPTIONS(NSUInteger, MGCStandardEventViewStyle) { /*! Detail - displayed with a smaller font and right aligned. */ @property (nonatomic, copy) NSString *detail; -/*! The color is used for background or text, depending on the style. */ -@property (nonatomic) UIColor *color; +/*! The color is used for text, depending on the style. */ +@property (nonatomic) UIColor *textColor; + +/*! The color is used for background, depending on the style. */ +@property (nonatomic) UIColor *eventNormalBackgroundColor; + +/*! The color is used for selected background, depending on the style. */ +@property (nonatomic) UIColor *eventSelectedBackgroundColor; /*! Style of the view. */ @property (nonatomic) MGCStandardEventViewStyle style; diff --git a/CalendarLib/MGCStandardEventView.m b/CalendarLib/MGCStandardEventView.m index f0e7b2fd..a50bfe4b 100755 --- a/CalendarLib/MGCStandardEventView.m +++ b/CalendarLib/MGCStandardEventView.m @@ -49,7 +49,9 @@ - (instancetype)initWithFrame:(CGRect)frame if (self = [super initWithFrame:frame]) { self.contentMode = UIViewContentModeRedraw; - _color = [UIColor blackColor]; + _textColor = [UIColor blackColor]; + _eventNormalBackgroundColor = [UIColor lightGrayColor]; + _eventSelectedBackgroundColor = [UIColor grayColor]; _style = MGCStandardEventViewStylePlain|MGCStandardEventViewStyleSubtitle; _font = [UIFont systemFontOfSize:[UIFont smallSystemFontSize]]; _leftBorderView = [[UIView alloc]initWithFrame:CGRectNull]; @@ -94,7 +96,7 @@ - (void)redrawStringInRect:(CGRect)rect //style.lineBreakMode = NSLineBreakByTruncatingMiddle; [as addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, as.length)]; - UIColor *color = self.selected ? [UIColor whiteColor] : self.color; + UIColor *color = self.selected ? [UIColor whiteColor] : self.textColor; [as addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, as.length)]; self.attrString = as; @@ -110,7 +112,7 @@ - (void)layoutSubviews - (void)setColor:(UIColor*)color { - _color = color; + _textColor = color; [self resetColors]; } @@ -123,12 +125,12 @@ - (void)setStyle:(MGCStandardEventViewStyle)style - (void)resetColors { - self.leftBorderView.backgroundColor = self.color; + self.leftBorderView.backgroundColor = self.textColor; if (self.selected) - self.backgroundColor = self.selected ? self.color : [self.color colorWithAlphaComponent:.3]; + self.backgroundColor = self.selected ? self.eventSelectedBackgroundColor : self.eventNormalBackgroundColor; else if (self.style & MGCStandardEventViewStylePlain) - self.backgroundColor = [self.color colorWithAlphaComponent:.3]; + self.backgroundColor = self.eventNormalBackgroundColor; else self.backgroundColor = [UIColor clearColor]; @@ -181,7 +183,9 @@ - (id)copyWithZone:(NSZone *)zone cell.title = self.title; cell.subtitle = self.subtitle; cell.detail = self.detail; - cell.color = self.color; + cell.textColor = self.textColor; + cell.eventNormalBackgroundColor = self.eventNormalBackgroundColor; + cell.eventSelectedBackgroundColor = self.eventSelectedBackgroundColor; cell.style = self.style; return cell;