diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 7e41c1c..97fac70 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -8,6 +8,9 @@ #import #import +NS_ASSUME_NONNULL_BEGIN + +extern const CGFloat PDTSimpleCalendarCircleSize; @class PDTSimpleCalendarViewCell; @@ -34,7 +37,7 @@ * * @return The text desired color */ -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date; +- (nullable UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date; /** * Asks the delegate for the circle color for a specific date. @@ -45,7 +48,7 @@ * * @return The circle desired color */ -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; +- (nullable UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; @end @@ -85,7 +88,7 @@ /** * Customize the day's number using UIAppearance. */ -@property (nonatomic, strong) UIColor *textDefaultColor UI_APPEARANCE_SELECTOR; +@property (nullable, nonatomic, strong) UIColor *textDefaultColor UI_APPEARANCE_SELECTOR; /** * Customize today's number color using UIAppearance. @@ -114,7 +117,14 @@ * * @param calendar the calendar. */ -- (void)setDate:(NSDate*)date calendar:(NSCalendar*)calendar; +- (void)setDate:(nullable NSDate*)date calendar:(nullable NSCalendar*)calendar; + +/** + * Set a non-standard Day Label in the cell. + * + * @param dayLabel The new label. + */ +- (void) setDayLabelText: (NSString *)text; /** * Force the refresh of the colors for the circle and the text @@ -122,3 +132,4 @@ - (void)refreshCellColors; @end +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index 36b01ef..07dbe18 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -19,6 +19,8 @@ @interface PDTSimpleCalendarViewCell () @implementation PDTSimpleCalendarViewCell +@synthesize textDefaultColor = _textDefaultColor; + #pragma mark - Class Methods + (NSString *)formatDate:(NSDate *)date withCalendar:(NSCalendar *)calendar @@ -107,6 +109,12 @@ - (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar self.dayLabel.accessibilityLabel = accessibilityDay; } +- (void) setDayLabelText: (NSString *)text { + + self.dayLabel.text = text; + self.dayLabel.accessibilityLabel = text; +} + - (void)setIsToday:(BOOL)isToday { _isToday = isToday; @@ -161,6 +169,8 @@ - (void)prepareForReuse [super prepareForReuse]; _date = nil; _isToday = NO; + _textDefaultColor = nil; + _textTodayColor = nil; [self.dayLabel setText:@""]; [self.dayLabel setBackgroundColor:[self circleDefaultColor]]; [self.dayLabel setTextColor:[self textDefaultColor]]; @@ -222,6 +232,12 @@ - (UIColor *)textDefaultColor return [UIColor blackColor]; } +- (void) setTextDefaultColor:(UIColor *)textDefaultColor +{ + _textDefaultColor = textDefaultColor ?: [[[self class] appearance] textDefaultColor]; + self.dayLabel.textColor = _textDefaultColor; +} + - (UIColor *)textTodayColor { if(_textTodayColor == nil) { diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h index 661e056..2008188 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h @@ -9,6 +9,7 @@ #import #import "PDTSimpleCalendarViewWeekdayHeader.h" +NS_ASSUME_NONNULL_BEGIN @protocol PDTSimpleCalendarViewDelegate; @@ -43,7 +44,7 @@ * Changing this value will not cause the calendar to scroll to this date. * You need to manually call scrollToSelectedDate:(BOOL)animated if you want this behavior. */ -@property (nonatomic, strong) NSDate *selectedDate; +@property (nullable, nonatomic, strong) NSDate *selectedDate; /** @name Customizing Appearance */ @@ -80,7 +81,7 @@ * * @see PDTSimpleCalendarViewDelegate */ -@property (nonatomic, weak) id delegate; +@property (nullable, nonatomic, weak) id delegate; /** @@ -144,7 +145,7 @@ * @param controller the calendarView Controller * @param date the date (Midnight GMT) */ -- (UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller circleColorForDate:(NSDate *)date; +- (nullable UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller circleColorForDate:(NSDate *)date; /** * Asks the delegate for the text color for a custom added date @@ -152,6 +153,15 @@ * @param controller the calendarView Controller * @param date the date (Midnight GMT) */ -- (UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller textColorForDate:(NSDate *)date; +- (nullable UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller textColorForDate:(NSDate *)date; + +/** + * Asks the delegate for custom text for a date + * + * @param controller the calendarView Controller + * @param date the date (Midnight GMT) + */ +- (nullable NSString *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller textForDate:(NSDate *)date; @end; +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 658e711..59d97db 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -172,7 +172,14 @@ - (NSDate *)lastDate - (void)setLastDate:(NSDate *)lastDate { - _lastDate = [self clampDate:lastDate toComponents:kCalendarUnitYMD]; + NSDate *clampedDate = [self clampDate:lastDate toComponents:kCalendarUnitYMD]; + + if ([_lastDate compare: clampedDate] != NSOrderedSame) { + + _lastDateMonth = nil; + [self.collectionViewLayout invalidateLayout]; + } + _lastDate = clampedDate; } - (NSDate *)lastDateMonth @@ -375,14 +382,27 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell if (cellDateComponents.month == firstOfMonthsComponents.month) { isSelected = ([self isSelectedDate:cellDate] && (indexPath.section == [self sectionForDate:cellDate])); isToday = [self isTodayDate:cellDate]; - [cell setDate:cellDate calendar:self.calendar]; + + //Ask the delegate if this date should have custom text. + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textForDate:)]) { + NSString * customText = [self.delegate simpleCalendarViewController:self textForDate:cellDate]; + if (customText) { + [cell setDayLabelText: customText]; + } + else { [cell setDate:cellDate calendar:self.calendar]; } + } + else { [cell setDate:cellDate calendar:self.calendar]; } + + //Ask the delegate if this date should have specific colors. + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) { + cell.textDefaultColor = [self.delegate simpleCalendarViewController:self textColorForDate:cellDate]; + } //Ask the delegate if this date should have specific colors. if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:shouldUseCustomColorsForDate:)]) { isCustomDate = [self.delegate simpleCalendarViewController:self shouldUseCustomColorsForDate:cellDate]; } - } else { [cell setDate:nil calendar:nil]; } @@ -397,6 +417,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell //If the current Date is not enabled, or if the delegate explicitely specify custom colors if (![self isEnabledDate:cellDate] || isCustomDate) { + cell.textDefaultColor = cell.textDisabledColor; [cell refreshCellColors]; } @@ -444,8 +465,14 @@ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView return headerView; } + else { - return nil; + PDTSimpleCalendarViewHeader *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:PDTSimpleCalendarViewHeaderIdentifier forIndexPath:indexPath]; + + headerView.bounds = CGRectZero; + + return headerView; + } } #pragma mark - UICollectionViewFlowLayoutDelegate @@ -454,7 +481,8 @@ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollection { CGFloat itemWidth = floorf(CGRectGetWidth(self.collectionView.bounds) / self.daysPerWeek); - return CGSizeMake(itemWidth, itemWidth); + // Limit the height with wide displays. + return CGSizeMake(itemWidth, MIN(itemWidth, round(PDTSimpleCalendarCircleSize * 1.2))); } #pragma mark - UIScrollViewDelegate @@ -612,27 +640,27 @@ - (BOOL)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell shouldUseCustom return NO; } -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date -{ +- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date { + if (![self isEnabledDate:date]) { - return cell.circleDefaultColor; + return cell.textDefaultColor; } - if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:circleColorForDate:)]) { - return [self.delegate simpleCalendarViewController:self circleColorForDate:date]; + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) { + return [self.delegate simpleCalendarViewController:self textColorForDate:date] ?: cell.textDefaultColor; } return nil; } -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date +- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date { if (![self isEnabledDate:date]) { - return cell.textDisabledColor; + return cell.circleDefaultColor; } - if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) { - return [self.delegate simpleCalendarViewController:self textColorForDate:date]; + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:circleColorForDate:)]) { + return [self.delegate simpleCalendarViewController:self circleColorForDate:date] ?: cell.circleDefaultColor; } return nil;