From 6e5d89987f0ac5ee0d4f9bdbb16ddd0601414fc3 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Sat, 3 Oct 2015 09:21:33 -0500 Subject: [PATCH 01/11] A pull request to start a discussion on how best to allow customization of cell sizes. --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 2 ++ PDTSimpleCalendar/PDTSimpleCalendarViewController.m | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 6d25a6d..7a55cd4 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -9,6 +9,8 @@ #import #import +extern const CGFloat PDTSimpleCalendarCircleSize; + @class PDTSimpleCalendarViewCell; @protocol PDTSimpleCalendarViewCellDelegate diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 1178475..a2094aa 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -423,7 +423,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 From 48afee4fe77221b2897dcf0dd3f433dbf21deb43 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Mon, 3 Oct 2016 07:45:06 -0500 Subject: [PATCH 02/11] Allow custom text per date and improve nullability annotations. --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 17 ++++-- PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 16 ++++++ .../PDTSimpleCalendarViewController.h | 18 +++++-- .../PDTSimpleCalendarViewController.m | 53 ++++++++++++++----- 4 files changed, 83 insertions(+), 21 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 7e41c1c..2d9c056 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -8,6 +8,7 @@ #import #import +NS_ASSUME_NONNULL_BEGIN @class PDTSimpleCalendarViewCell; @@ -34,7 +35,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 +46,7 @@ * * @return The circle desired color */ -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; +- (nullable UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; @end @@ -85,7 +86,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 +115,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 +130,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..fb106de 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 @@ -612,27 +639,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; From 79404d30f2f251b2ea2d09024dfdb79e38d36765 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Fri, 6 Jan 2017 16:28:41 -0600 Subject: [PATCH 03/11] Cosmetic edits. --- PDTSimpleCalendar/PDTSimpleCalendarViewController.m | 1 - 1 file changed, 1 deletion(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 59d97db..d3cff25 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -523,7 +523,6 @@ - (void)hideOverlayView }]; } -#pragma mark - #pragma mark - Calendar calculations - (NSDate *)clampDate:(NSDate *)date toComponents:(NSUInteger)unitFlags From e9495202bc02d3c942d70cd797d750718427eed5 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Tue, 16 May 2017 13:36:39 -0500 Subject: [PATCH 04/11] Modernize accessors. --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 2 +- PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 81 ++++++------------- 2 files changed, 25 insertions(+), 58 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 97fac70..c43d435 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -88,7 +88,7 @@ extern const CGFloat PDTSimpleCalendarCircleSize; /** * Customize the day's number using UIAppearance. */ -@property (nullable, nonatomic, strong) UIColor *textDefaultColor UI_APPEARANCE_SELECTOR; +@property (nonatomic, strong) UIColor *textDefaultColor UI_APPEARANCE_SELECTOR; /** * Customize today's number color using UIAppearance. diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index 07dbe18..caf58f0 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -180,56 +180,40 @@ - (void)prepareForReuse - (UIColor *)circleDefaultColor { - if(_circleDefaultColor == nil) { - _circleDefaultColor = [[[self class] appearance] circleDefaultColor]; - } + if(_circleDefaultColor) { return _circleDefaultColor; } - if(_circleDefaultColor != nil) { - return _circleDefaultColor; - } + _circleDefaultColor = [[[self class] appearance] circleDefaultColor]; - return [UIColor whiteColor]; + return _circleDefaultColor; } - (UIColor *)circleTodayColor { - if(_circleTodayColor == nil) { - _circleTodayColor = [[[self class] appearance] circleTodayColor]; - } + if(_circleTodayColor) { return _circleTodayColor; } - if(_circleTodayColor != nil) { - return _circleTodayColor; - } + _circleTodayColor = [[[self class] appearance] circleTodayColor]; - return [UIColor grayColor]; + return _circleTodayColor; } - (UIColor *)circleSelectedColor { - if(_circleSelectedColor == nil) { - _circleSelectedColor = [[[self class] appearance] circleSelectedColor]; - } + if(_circleSelectedColor) { return _circleSelectedColor; } - if(_circleSelectedColor != nil) { - return _circleSelectedColor; - } + _circleSelectedColor = [[[self class] appearance] circleSelectedColor]; - return [UIColor redColor]; + return _circleSelectedColor; } #pragma mark - Text Label Customizations Color - (UIColor *)textDefaultColor { - if(_textDefaultColor == nil) { - _textDefaultColor = [[[self class] appearance] textDefaultColor]; - } + if(_textDefaultColor) { return _textDefaultColor; } - if(_textDefaultColor != nil) { - return _textDefaultColor; - } + _textDefaultColor = [[[self class] appearance] textDefaultColor]; - return [UIColor blackColor]; + return _textDefaultColor; } - (void) setTextDefaultColor:(UIColor *)textDefaultColor @@ -240,57 +224,40 @@ - (void) setTextDefaultColor:(UIColor *)textDefaultColor - (UIColor *)textTodayColor { - if(_textTodayColor == nil) { - _textTodayColor = [[[self class] appearance] textTodayColor]; - } + if(_textTodayColor) { return _textTodayColor; } - if(_textTodayColor != nil) { - return _textTodayColor; - } + _textTodayColor = [[[self class] appearance] textTodayColor]; - return [UIColor whiteColor]; + return _textTodayColor; } - (UIColor *)textSelectedColor { - if(_textSelectedColor == nil) { - _textSelectedColor = [[[self class] appearance] textSelectedColor]; - } + if(_textSelectedColor) { return _textSelectedColor; } - if(_textSelectedColor != nil) { - return _textSelectedColor; - } + _textSelectedColor = [[[self class] appearance] textSelectedColor]; - return [UIColor whiteColor]; + return _textSelectedColor; } - (UIColor *)textDisabledColor { - if(_textDisabledColor == nil) { - _textDisabledColor = [[[self class] appearance] textDisabledColor]; - } + if(_textDisabledColor) { return _textDisabledColor; } - if(_textDisabledColor != nil) { - return _textDisabledColor; - } + _textDisabledColor = [[[self class] appearance] textDisabledColor]; - return [UIColor lightGrayColor]; + return _textDisabledColor; } #pragma mark - Text Label Customizations Font - (UIFont *)textDefaultFont { - if(_textDefaultFont == nil) { - _textDefaultFont = [[[self class] appearance] textDefaultFont]; - } + if(_textDefaultFont) { return _textDefaultFont; } - if (_textDefaultFont != nil) { - return _textDefaultFont; - } + _textDefaultFont = [[[self class] appearance] textDefaultFont]; - // default system font - return [UIFont systemFontOfSize:17.0]; + return _textDefaultFont; } @end From 0de5b304db156d47cb22092b95760db48898c952 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Tue, 16 May 2017 14:02:09 -0500 Subject: [PATCH 05/11] Reorganizing code. Improving locality. --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index caf58f0..c0160c2 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -19,8 +19,6 @@ @interface PDTSimpleCalendarViewCell () @implementation PDTSimpleCalendarViewCell -@synthesize textDefaultColor = _textDefaultColor; - #pragma mark - Class Methods + (NSString *)formatDate:(NSDate *)date withCalendar:(NSCalendar *)calendar @@ -207,6 +205,8 @@ - (UIColor *)circleSelectedColor #pragma mark - Text Label Customizations Color +@synthesize textDefaultColor = _textDefaultColor; + - (UIColor *)textDefaultColor { if(_textDefaultColor) { return _textDefaultColor; } From e49bdb9aa2530546a2e759ed518fbb05d63908c1 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Sat, 3 Jun 2017 12:41:17 -0500 Subject: [PATCH 06/11] Modernize and use the UIAppearance proxy better. --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 3 +- PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 107 +++++------------- .../PDTSimpleCalendarViewController.h | 2 +- .../PDTSimpleCalendarViewFlowLayout.h | 2 +- .../PDTSimpleCalendarViewHeader.h | 2 +- .../PDTSimpleCalendarViewHeader.m | 55 ++------- .../PDTSimpleCalendarViewWeekdayHeader.h | 2 +- .../PDTSimpleCalendarViewWeekdayHeader.m | 52 +++------ 8 files changed, 59 insertions(+), 166 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index c43d435..e88b97a 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -6,8 +6,7 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import -#import +@import UIKit; NS_ASSUME_NONNULL_BEGIN extern const CGFloat PDTSimpleCalendarCircleSize; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index c0160c2..f0dd4fd 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -21,6 +21,23 @@ @implementation PDTSimpleCalendarViewCell #pragma mark - Class Methods ++ (void)initialize { + // Set the UIAppearance default values. + if (self == [PDTSimpleCalendarViewCell class]) { + PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance]; + + [proxy setCircleDefaultColor: [UIColor whiteColor]]; + [proxy setCircleTodayColor: [UIColor grayColor]]; + [proxy setCircleSelectedColor:[UIColor redColor]]; + + [proxy setTextDefaultColor: [UIColor blackColor]]; + [proxy setTextTodayColor: [UIColor whiteColor]]; + [proxy setTextSelectedColor: [UIColor whiteColor]]; + [proxy setTextDisabledColor: [UIColor lightGrayColor]]; + [proxy setTextDefaultFont: [UIFont systemFontOfSize: 17.0]]; + } +} + + (NSString *)formatDate:(NSDate *)date withCalendar:(NSCalendar *)calendar { NSDateFormatter *dateFormatter = [self dateFormatter]; @@ -90,7 +107,6 @@ - (id)initWithFrame:(CGRect)frame [self setCircleColor:NO selected:NO]; } - return self; } @@ -167,97 +183,32 @@ - (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]]; -} - -#pragma mark - Circle Color Customization Methods - -- (UIColor *)circleDefaultColor -{ - if(_circleDefaultColor) { return _circleDefaultColor; } - - _circleDefaultColor = [[[self class] appearance] circleDefaultColor]; - - return _circleDefaultColor; -} - -- (UIColor *)circleTodayColor -{ - if(_circleTodayColor) { return _circleTodayColor; } - _circleTodayColor = [[[self class] appearance] circleTodayColor]; + PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance]; - return _circleTodayColor; -} - -- (UIColor *)circleSelectedColor -{ - if(_circleSelectedColor) { return _circleSelectedColor; } + self.circleDefaultColor = proxy.circleDefaultColor; + self.circleTodayColor = proxy.circleTodayColor; + self.circleSelectedColor = proxy.circleSelectedColor; - _circleSelectedColor = [[[self class] appearance] circleSelectedColor]; + self.textDefaultColor = proxy.textDefaultColor; + self.textTodayColor = proxy.textTodayColor; + self.textSelectedColor = proxy.textSelectedColor; + self.textDisabledColor = proxy.textDisabledColor; + self.textDefaultFont = proxy.textDefaultFont; - return _circleSelectedColor; + [self.dayLabel setText:@""]; + [self.dayLabel setBackgroundColor: proxy.circleDefaultColor]; + [self.dayLabel setTextColor: proxy.textDefaultColor]; } #pragma mark - Text Label Customizations Color @synthesize textDefaultColor = _textDefaultColor; -- (UIColor *)textDefaultColor -{ - if(_textDefaultColor) { return _textDefaultColor; } - - _textDefaultColor = [[[self class] appearance] textDefaultColor]; - - return _textDefaultColor; -} - - (void) setTextDefaultColor:(UIColor *)textDefaultColor { _textDefaultColor = textDefaultColor ?: [[[self class] appearance] textDefaultColor]; self.dayLabel.textColor = _textDefaultColor; } -- (UIColor *)textTodayColor -{ - if(_textTodayColor) { return _textTodayColor; } - - _textTodayColor = [[[self class] appearance] textTodayColor]; - - return _textTodayColor; -} - -- (UIColor *)textSelectedColor -{ - if(_textSelectedColor) { return _textSelectedColor; } - - _textSelectedColor = [[[self class] appearance] textSelectedColor]; - - return _textSelectedColor; -} - -- (UIColor *)textDisabledColor -{ - if(_textDisabledColor) { return _textDisabledColor; } - - _textDisabledColor = [[[self class] appearance] textDisabledColor]; - - return _textDisabledColor; -} - -#pragma mark - Text Label Customizations Font - -- (UIFont *)textDefaultFont -{ - if(_textDefaultFont) { return _textDefaultFont; } - - _textDefaultFont = [[[self class] appearance] textDefaultFont]; - - return _textDefaultFont; -} - @end diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h index 2008188..2ce59ae 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h @@ -6,7 +6,7 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import +@import UIKit; #import "PDTSimpleCalendarViewWeekdayHeader.h" NS_ASSUME_NONNULL_BEGIN diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h b/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h index 61a3c5b..74d0708 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h @@ -6,7 +6,7 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import +@import UIKit; extern const CGFloat PDTSimpleCalendarFlowLayoutMinInterItemSpacing; extern const CGFloat PDTSimpleCalendarFlowLayoutMinLineSpacing; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h index 377304b..fd2ca23 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h @@ -6,7 +6,7 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import +@import UIKit; /** * The `PDTSimpleCalendarViewHeader` class displays the month name and year. diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m index 92bf498..26dd649 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m @@ -12,6 +12,18 @@ @implementation PDTSimpleCalendarViewHeader ++ (void)initialize { + // Set the UIAppearance default values. + if (self == [PDTSimpleCalendarViewHeader class]) { + PDTSimpleCalendarViewHeader *proxy = [PDTSimpleCalendarViewHeader appearance]; + + [proxy setSeparatorColor: [UIColor lightGrayColor]]; + + [proxy setTextColor: [UIColor blackColor]]; + [proxy setTextFont: [UIFont systemFontOfSize: PDTSimpleCalendarHeaderTextSize]]; + } +} + - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; @@ -44,47 +56,4 @@ - (id)initWithFrame:(CGRect)frame return self; } - -#pragma mark - Colors - -- (UIColor *)textColor -{ - if(_textColor == nil) { - _textColor = [[[self class] appearance] textColor]; - } - - if(_textColor != nil) { - return _textColor; - } - - return [UIColor grayColor]; -} - -- (UIFont *)textFont -{ - if(_textFont == nil) { - _textFont = [[[self class] appearance] textFont]; - } - - if(_textFont != nil) { - return _textFont; - } - - return [UIFont systemFontOfSize:PDTSimpleCalendarHeaderTextSize]; -} - -- (UIColor *)separatorColor -{ - if(_separatorColor == nil) { - _separatorColor = [[[self class] appearance] separatorColor]; - } - - if(_separatorColor != nil) { - return _separatorColor; - } - - return [UIColor lightGrayColor]; -} - - @end diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h index 03f832a..1899eaf 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h @@ -6,7 +6,7 @@ // Copyright (c) 2015 MorningCall. All rights reserved. // -#import +@import UIKit; extern const CGFloat PDTSimpleCalendarWeekdayHeaderSize; extern const CGFloat PDTSimpleCalendarWeekdayHeaderHeight; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m index 2d1a4d4..445817c 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m @@ -13,6 +13,19 @@ @implementation PDTSimpleCalendarViewWeekdayHeader ++ (void)initialize { + // Set the UIAppearance default values. + if (self == [PDTSimpleCalendarViewWeekdayHeader class]) { + PDTSimpleCalendarViewWeekdayHeader *proxy = [PDTSimpleCalendarViewWeekdayHeader appearance]; + + [proxy setHeaderBackgroundColor: [UIColor whiteColor]]; + + [proxy setTextColor: [UIColor blackColor]]; + [proxy setTextFont: [UIFont systemFontOfSize: PDTSimpleCalendarWeekdayHeaderSize]]; + } +} + + /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. @@ -91,43 +104,4 @@ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendar return self; } -- (UIColor *)textColor -{ - if(_textColor == nil) { - _textColor = [[[self class] appearance] textColor]; - } - - if(_textColor != nil) { - return _textColor; - } - - return [UIColor blackColor]; -} - -- (UIFont *)textFont -{ - if(_textFont == nil) { - _textFont = [[[self class] appearance] textFont]; - } - - if(_textFont != nil) { - return _textFont; - } - - return [UIFont systemFontOfSize:PDTSimpleCalendarWeekdayHeaderSize]; -} - -- (UIColor *)headerBackgroundColor -{ - if(_headerBackgroundColor == nil) { - _headerBackgroundColor = [[[self class] appearance] headerBackgroundColor]; - } - - if(_headerBackgroundColor != nil) { - return _headerBackgroundColor; - } - - return [UIColor whiteColor]; -} - @end From babc3f5403a93569b1e9786e7c2838ad7f8dc115 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Sat, 3 Jun 2017 15:49:10 -0500 Subject: [PATCH 07/11] Modernize and use the UIAppearance proxy better. --- PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h | 2 ++ PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h | 2 ++ PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h | 2 ++ PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m | 1 - 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h b/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h index 74d0708..98e36aa 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h @@ -7,6 +7,7 @@ // @import UIKit; +NS_ASSUME_NONNULL_BEGIN extern const CGFloat PDTSimpleCalendarFlowLayoutMinInterItemSpacing; extern const CGFloat PDTSimpleCalendarFlowLayoutMinLineSpacing; @@ -19,3 +20,4 @@ extern const CGFloat PDTSimpleCalendarFlowLayoutHeaderHeight; @interface PDTSimpleCalendarViewFlowLayout : UICollectionViewFlowLayout @end +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h index fd2ca23..b394115 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h @@ -7,6 +7,7 @@ // @import UIKit; +NS_ASSUME_NONNULL_BEGIN /** * The `PDTSimpleCalendarViewHeader` class displays the month name and year. @@ -36,3 +37,4 @@ @property (nonatomic, strong) UIColor *separatorColor UI_APPEARANCE_SELECTOR; @end +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h index 1899eaf..e4e074f 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h @@ -7,6 +7,7 @@ // @import UIKit; +NS_ASSUME_NONNULL_BEGIN extern const CGFloat PDTSimpleCalendarWeekdayHeaderSize; extern const CGFloat PDTSimpleCalendarWeekdayHeaderHeight; @@ -43,3 +44,4 @@ typedef NS_ENUM (NSInteger, PDTSimpleCalendarViewWeekdayTextType) { @property (nonatomic, strong) UIColor *headerBackgroundColor UI_APPEARANCE_SELECTOR; @end +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m index 445817c..7850296 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m @@ -25,7 +25,6 @@ + (void)initialize { } } - /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. From 144d3073267feca1e1b6753a8e588b8db4a20189 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Mon, 12 Jun 2017 10:00:59 -0700 Subject: [PATCH 08/11] Remove and move code to support UIAppearanceContainer protocol properly. Much discussion was had over this code at WWDC17. --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 12 +- PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 110 ++++++++++-------- .../PDTSimpleCalendarViewController.m | 14 +-- .../PDTSimpleCalendarViewHeader.h | 2 +- .../PDTSimpleCalendarViewHeader.m | 42 ++++--- .../PDTSimpleCalendarViewWeekdayHeader.h | 4 +- .../PDTSimpleCalendarViewWeekdayHeader.m | 51 +++++--- 7 files changed, 134 insertions(+), 101 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index e88b97a..9da3407 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -9,7 +9,7 @@ @import UIKit; NS_ASSUME_NONNULL_BEGIN -extern const CGFloat PDTSimpleCalendarCircleSize; +extern const CGFloat kPDTSimpleCalendarCircleSize; @class PDTSimpleCalendarViewCell; @@ -54,7 +54,7 @@ extern const CGFloat PDTSimpleCalendarCircleSize; /** * The `PDTSimpleCalendarViewCell` class displays a day in the calendar. */ -@interface PDTSimpleCalendarViewCell : UICollectionViewCell +@interface PDTSimpleCalendarViewCell : UICollectionViewCell /** * The delegate of the cell. @@ -67,7 +67,7 @@ extern const CGFloat PDTSimpleCalendarCircleSize; /** * Define if the cell is today in the calendar. */ -@property (nonatomic, assign) BOOL isToday; +@property (nonatomic, assign, getter=isToday) BOOL today; /** * Customize the circle behind the day's number color using UIAppearance. @@ -119,11 +119,9 @@ extern const CGFloat PDTSimpleCalendarCircleSize; - (void)setDate:(nullable NSDate*)date calendar:(nullable NSCalendar*)calendar; /** - * Set a non-standard Day Label in the cell. - * - * @param dayLabel The new label. + * Set the text for this cell. */ -- (void) setDayLabelText: (NSString *)text; +@property (nonatomic, strong) NSString *text; /** * Force the refresh of the colors for the circle and the text diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index f0dd4fd..dafce79 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -8,7 +8,7 @@ #import "PDTSimpleCalendarViewCell.h" -const CGFloat PDTSimpleCalendarCircleSize = 32.0f; +const CGFloat kPDTSimpleCalendarCircleSize = 32.0f; @interface PDTSimpleCalendarViewCell () @@ -22,19 +22,19 @@ @implementation PDTSimpleCalendarViewCell #pragma mark - Class Methods + (void)initialize { - // Set the UIAppearance default values. if (self == [PDTSimpleCalendarViewCell class]) { + // Set the UIAppearance default values. PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance]; - [proxy setCircleDefaultColor: [UIColor whiteColor]]; - [proxy setCircleTodayColor: [UIColor grayColor]]; - [proxy setCircleSelectedColor:[UIColor redColor]]; + proxy.circleDefaultColor = [UIColor whiteColor]; + proxy.circleTodayColor = [UIColor grayColor]; + proxy.circleSelectedColor = [UIColor redColor]; - [proxy setTextDefaultColor: [UIColor blackColor]]; - [proxy setTextTodayColor: [UIColor whiteColor]]; - [proxy setTextSelectedColor: [UIColor whiteColor]]; - [proxy setTextDisabledColor: [UIColor lightGrayColor]]; - [proxy setTextDefaultFont: [UIFont systemFontOfSize: 17.0]]; + proxy.textDefaultColor = [UIColor blackColor]; + proxy.textTodayColor = [UIColor whiteColor]; + proxy.textSelectedColor = [UIColor whiteColor]; + proxy.textDisabledColor = [UIColor lightGrayColor]; + proxy.textDefaultFont = [UIFont systemFontOfSize: 17.0]; } } @@ -50,7 +50,6 @@ + (NSString *)formatAccessibilityDate:(NSDate *)date withCalendar:(NSCalendar *) return [PDTSimpleCalendarViewCell stringFromDate:date withDateFormatter:dateFormatter withCalendar:calendar]; } - + (NSDateFormatter *)dateFormatter { static NSDateFormatter *dateFormatter; static dispatch_once_t onceToken; @@ -68,7 +67,6 @@ + (NSDateFormatter *)accessibilityDateFormatter { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; - }); return dateFormatter; } @@ -83,31 +81,39 @@ + (NSString *)stringFromDate:(NSDate *)date withDateFormatter:(NSDateFormatter * #pragma mark - Instance Methods -- (id)initWithFrame:(CGRect)frame +- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _date = nil; - _isToday = NO; + _today = NO; _dayLabel = [[UILabel alloc] init]; - [self.dayLabel setFont:[self textDefaultFont]]; - [self.dayLabel setTextAlignment:NSTextAlignmentCenter]; - [self.contentView addSubview:self.dayLabel]; + [_dayLabel setTextAlignment: NSTextAlignmentCenter]; + [self.contentView addSubview: _dayLabel]; //Add the Constraints - [self.dayLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; - [self.dayLabel setBackgroundColor:[UIColor clearColor]]; - self.dayLabel.layer.cornerRadius = PDTSimpleCalendarCircleSize/2; - self.dayLabel.layer.masksToBounds = YES; + [_dayLabel setTranslatesAutoresizingMaskIntoConstraints: NO]; + [_dayLabel setBackgroundColor:[UIColor clearColor]]; + _dayLabel.layer.cornerRadius = kPDTSimpleCalendarCircleSize/2; + _dayLabel.layer.masksToBounds = YES; + + [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self.contentView attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0.0]]; + [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem: self.contentView attribute: NSLayoutAttributeCenterY multiplier: 1.0 constant: 0.0]]; + [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: kPDTSimpleCalendarCircleSize]]; + [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: kPDTSimpleCalendarCircleSize]]; + } + return self; +} - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]]; - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize]]; - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize]]; +- (void) didMoveToWindow { + // Use the UIAppearance properties only after they been finalized by being + // inserted into a live window. + [super didMoveToWindow]; - [self setCircleColor:NO selected:NO]; + if (self.window) { + [self.dayLabel setFont: self.textDefaultFont]; + [self setCircleColor: self.isToday selected: self.selected]; } - return self; } - (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar @@ -123,16 +129,23 @@ - (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar self.dayLabel.accessibilityLabel = accessibilityDay; } -- (void) setDayLabelText: (NSString *)text { +@dynamic text; + +- (NSString *) text { + + return self.dayLabel.text; +} + +- (void) setText: (NSString *)text { self.dayLabel.text = text; self.dayLabel.accessibilityLabel = text; } -- (void)setIsToday:(BOOL)isToday +- (void) setToday: (BOOL) today { - _isToday = isToday; - [self setCircleColor:isToday selected:self.selected]; + _today = today; + [self setCircleColor: today selected: self.selected]; } - (void)setSelected:(BOOL)selected @@ -159,53 +172,48 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected } } } - if (selected) { circleColor = [self circleSelectedColor]; labelColor = [self textSelectedColor]; } - [self.dayLabel setBackgroundColor:circleColor]; [self.dayLabel setTextColor:labelColor]; } - - (void)refreshCellColors { [self setCircleColor:self.isToday selected:self.isSelected]; } - #pragma mark - Prepare for Reuse - (void)prepareForReuse { [super prepareForReuse]; - _date = nil; - _isToday = NO; + self.date = nil; + self.today = NO; + // TODO: File a Doc bug, Thaddeus Cooper in DTS. Quinn & Larry, Kirby Turner. PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance]; - self.circleDefaultColor = proxy.circleDefaultColor; - self.circleTodayColor = proxy.circleTodayColor; - self.circleSelectedColor = proxy.circleSelectedColor; + if (_circleDefaultColor) { self.circleDefaultColor = proxy.circleDefaultColor; } + if (_circleTodayColor) { self.circleTodayColor = proxy.circleTodayColor; } + if (_circleSelectedColor) { self.circleSelectedColor = proxy.circleSelectedColor; } - self.textDefaultColor = proxy.textDefaultColor; - self.textTodayColor = proxy.textTodayColor; - self.textSelectedColor = proxy.textSelectedColor; - self.textDisabledColor = proxy.textDisabledColor; - self.textDefaultFont = proxy.textDefaultFont; + if (_textDefaultColor) { self.textDefaultColor = proxy.textDefaultColor; } + if (_textTodayColor) { self.textTodayColor = proxy.textTodayColor; } + if (_textSelectedColor) { self.textSelectedColor = proxy.textSelectedColor; } + if (_textDisabledColor) { self.textDisabledColor = proxy.textDisabledColor; } + if (_textDefaultFont) { self.textDefaultFont = proxy.textDefaultFont; } - [self.dayLabel setText:@""]; - [self.dayLabel setBackgroundColor: proxy.circleDefaultColor]; - [self.dayLabel setTextColor: proxy.textDefaultColor]; + self.dayLabel.text = @""; + self.dayLabel.backgroundColor = proxy.circleDefaultColor; + self.dayLabel.textColor = proxy.textDefaultColor; } #pragma mark - Text Label Customizations Color -@synthesize textDefaultColor = _textDefaultColor; - -- (void) setTextDefaultColor:(UIColor *)textDefaultColor +- (void) setTextDefaultColor: (UIColor *) textDefaultColor { _textDefaultColor = textDefaultColor ?: [[[self class] appearance] textDefaultColor]; self.dayLabel.textColor = _textDefaultColor; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index d3cff25..40dab83 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -387,7 +387,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textForDate:)]) { NSString * customText = [self.delegate simpleCalendarViewController:self textForDate:cellDate]; if (customText) { - [cell setDayLabelText: customText]; + [cell setText: customText]; } else { [cell setDate:cellDate calendar:self.calendar]; } } @@ -406,14 +406,8 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell } else { [cell setDate:nil calendar:nil]; } - - if (isToday) { - [cell setIsToday:isToday]; - } - - if (isSelected) { - [cell setSelected:isSelected]; - } + [cell setToday: isToday]; + [cell setSelected: isSelected]; //If the current Date is not enabled, or if the delegate explicitely specify custom colors if (![self isEnabledDate:cellDate] || isCustomDate) { @@ -482,7 +476,7 @@ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollection CGFloat itemWidth = floorf(CGRectGetWidth(self.collectionView.bounds) / self.daysPerWeek); // Limit the height with wide displays. - return CGSizeMake(itemWidth, MIN(itemWidth, round(PDTSimpleCalendarCircleSize * 1.2))); + return CGSizeMake(itemWidth, MIN(itemWidth, round(kPDTSimpleCalendarCircleSize * 1.2))); } #pragma mark - UIScrollViewDelegate diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h index b394115..ae4299b 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN /** * The `PDTSimpleCalendarViewHeader` class displays the month name and year. */ -@interface PDTSimpleCalendarViewHeader : UICollectionReusableView +@interface PDTSimpleCalendarViewHeader : UICollectionReusableView /** * Label that display the month and year diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m index 26dd649..c2484fb 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m @@ -8,43 +8,44 @@ #import "PDTSimpleCalendarViewHeader.h" -const CGFloat PDTSimpleCalendarHeaderTextSize = 12.0f; +const CGFloat kPDTSimpleCalendarHeaderTextSize = 12.0f; + +@interface PDTSimpleCalendarViewHeader () + +@property (nonatomic, nonnull, retain) UIView *separatorView; + +@end @implementation PDTSimpleCalendarViewHeader + (void)initialize { - // Set the UIAppearance default values. if (self == [PDTSimpleCalendarViewHeader class]) { + // Set the UIAppearance default values. PDTSimpleCalendarViewHeader *proxy = [PDTSimpleCalendarViewHeader appearance]; [proxy setSeparatorColor: [UIColor lightGrayColor]]; [proxy setTextColor: [UIColor blackColor]]; - [proxy setTextFont: [UIFont systemFontOfSize: PDTSimpleCalendarHeaderTextSize]]; + [proxy setTextFont: [UIFont systemFontOfSize: kPDTSimpleCalendarHeaderTextSize]]; } } -- (id)initWithFrame:(CGRect)frame +- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code _titleLabel = [[UILabel alloc] init]; - [_titleLabel setFont:self.textFont]; - [_titleLabel setTextColor:self.textColor]; - [_titleLabel setBackgroundColor:[UIColor clearColor]]; - [self addSubview:_titleLabel]; [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; - UIView *separatorView = [[UIView alloc] init]; - [separatorView setBackgroundColor:self.separatorColor]; - [self addSubview:separatorView]; - [separatorView setTranslatesAutoresizingMaskIntoConstraints:NO]; + _separatorView = [[UIView alloc] init]; + [self addSubview: _separatorView]; + [_separatorView setTranslatesAutoresizingMaskIntoConstraints:NO]; CGFloat onePixel = 1.0f / [UIScreen mainScreen].scale; NSDictionary *metricsDictionary = @{@"onePixel" : [NSNumber numberWithFloat:onePixel]}; - NSDictionary *viewsDictionary = @{@"titleLabel" : self.titleLabel, @"separatorView" : separatorView}; + NSDictionary *viewsDictionary = @{@"titleLabel" : _titleLabel, @"separatorView" : _separatorView}; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(==10)-[titleLabel]-(==10)-|" options:0 metrics:nil views:viewsDictionary]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[titleLabel]|" options:0 metrics:nil views:viewsDictionary]]; @@ -56,4 +57,19 @@ - (id)initWithFrame:(CGRect)frame return self; } +- (void) didMoveToWindow { + + [super didMoveToWindow]; + + if (self.window) { + // Use the UIAppearance properties only after they been finalized by being + // inserted into a live window. + self.separatorView.backgroundColor = self.separatorColor; + + self.titleLabel.font = self.textFont; + self.titleLabel.textColor = self.textColor; + self.titleLabel.backgroundColor = UIColor.clearColor; + } +} + @end diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h index e4e074f..8679108 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h @@ -18,13 +18,13 @@ typedef NS_ENUM (NSInteger, PDTSimpleCalendarViewWeekdayTextType) { PDTSimpleCalendarViewWeekdayTextTypeStandAlone }; -@interface PDTSimpleCalendarViewWeekdayHeader : UIView +@interface PDTSimpleCalendarViewWeekdayHeader : UIView /** * Init with calendar * * @param calendar the calendar used to generate the view. - * @param textType + * @param textType The text style. */ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendarViewWeekdayTextType)textType; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m index 7850296..fe4c102 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m @@ -11,6 +11,14 @@ const CGFloat PDTSimpleCalendarWeekdayHeaderSize = 12.0f; const CGFloat PDTSimpleCalendarWeekdayHeaderHeight = 20.0f; +@interface PDTSimpleCalendarViewWeekdayHeader () + +@property (nonatomic, retain) NSCalendar *calendar; +@property (nonatomic) PDTSimpleCalendarViewWeekdayTextType weekdayTextType; +@property (nonatomic, retain) NSArray *weekdaySymbols; + +@end + @implementation PDTSimpleCalendarViewWeekdayHeader + (void)initialize { @@ -38,11 +46,14 @@ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendar self = [super init]; if (self) { - self.backgroundColor = self.headerBackgroundColor; + self.calendar = calendar; + self.weekdayTextType = textType; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.calendar = calendar; + NSArray *weekdaySymbols = nil; - + switch (textType) { case PDTSimpleCalendarViewWeekdayTextTypeVeryShort: weekdaySymbols = [dateFormatter veryShortWeekdaySymbols]; @@ -54,27 +65,37 @@ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendar weekdaySymbols = [dateFormatter standaloneWeekdaySymbols]; break; } - - NSMutableArray *adjustedSymbols = [NSMutableArray arrayWithArray:weekdaySymbols]; - for (NSInteger index = 0; index < (1 - calendar.firstWeekday + weekdaySymbols.count); index++) { + self.weekdaySymbols = weekdaySymbols; + } + return self; +} + +- (void) didMoveToWindow { + + [super didMoveToWindow]; + + if (self.window) { + // Use the UIAppearance properties only after they been finalized by being + // inserted into a live window. + self.backgroundColor = self.headerBackgroundColor; + + NSMutableArray *adjustedSymbols = [NSMutableArray arrayWithArray: self.weekdaySymbols]; + for (NSInteger index = 0; index < (1 - self.calendar.firstWeekday + self.weekdaySymbols.count); index++) { NSString *lastObject = [adjustedSymbols lastObject]; [adjustedSymbols removeLastObject]; [adjustedSymbols insertObject:lastObject atIndex:0]; } - if (adjustedSymbols.count == 0) { - return self; + return; } - UILabel *firstWeekdaySymbolLabel = nil; - NSMutableArray *weekdaySymbolLabelNameArr = [NSMutableArray array]; NSMutableDictionary *weekdaySymbolLabelDict = [NSMutableDictionary dictionary]; for (NSInteger index = 0; index < adjustedSymbols.count; index++) { NSString *labelName = [NSString stringWithFormat:@"weekdaySymbolLabel%d", (int)index]; [weekdaySymbolLabelNameArr addObject:labelName]; - + UILabel *weekdaySymbolLabel = [[UILabel alloc] init]; weekdaySymbolLabel.font = self.textFont; weekdaySymbolLabel.text = [adjustedSymbols[index] uppercaseString]; @@ -82,25 +103,21 @@ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendar weekdaySymbolLabel.textAlignment = NSTextAlignmentCenter; weekdaySymbolLabel.backgroundColor = [UIColor clearColor]; weekdaySymbolLabel.translatesAutoresizingMaskIntoConstraints = NO; - + [self addSubview:weekdaySymbolLabel]; - + [weekdaySymbolLabelDict setObject:weekdaySymbolLabel forKey:labelName]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|[%@]|", labelName] options:0 metrics:nil views:weekdaySymbolLabelDict]]; - + if (firstWeekdaySymbolLabel == nil) { firstWeekdaySymbolLabel = weekdaySymbolLabel; } else { [self addConstraint:[NSLayoutConstraint constraintWithItem:weekdaySymbolLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:firstWeekdaySymbolLabel attribute:NSLayoutAttributeWidth multiplier:1 constant:0]]; } } - NSString *layoutString = [NSString stringWithFormat:@"|[%@(>=0)]|", [weekdaySymbolLabelNameArr componentsJoinedByString:@"]["]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:layoutString options:NSLayoutFormatAlignAllCenterY metrics:nil views:weekdaySymbolLabelDict]]; - } - - return self; } @end From cb2cfd25e0a984fb7bdfac9cb28f3dd1c5d6c59e Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Wed, 21 Jun 2017 12:44:18 -0500 Subject: [PATCH 09/11] Reorganize file. --- .../PDTSimpleCalendarViewController.m | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 40dab83..4f24950 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -633,27 +633,27 @@ - (BOOL)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell shouldUseCustom return NO; } -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date { - +- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date +{ if (![self isEnabledDate:date]) { - return cell.textDefaultColor; + return cell.circleDefaultColor; } - if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) { - return [self.delegate simpleCalendarViewController:self textColorForDate:date] ?: cell.textDefaultColor; + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:circleColorForDate:)]) { + return [self.delegate simpleCalendarViewController:self circleColorForDate:date] ?: cell.circleDefaultColor; } return nil; } -- (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] ?: cell.circleDefaultColor; + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) { + return [self.delegate simpleCalendarViewController:self textColorForDate:date] ?: cell.textDefaultColor; } return nil; From ebdd4964aed6dc88b29d7a9d5bffa5054cedd3ff Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Wed, 21 Jun 2017 12:51:15 -0500 Subject: [PATCH 10/11] Improve memory management. --- .../PDTSimpleCalendarViewWeekdayHeader.m | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m index fe4c102..22b508e 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m @@ -33,25 +33,16 @@ + (void)initialize { } } -/* -// Only override drawRect: if you perform custom drawing. -// An empty implementation adversely affects performance during animation. -- (void)drawRect:(CGRect)rect { - // Drawing code -} -*/ - - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendarViewWeekdayTextType)textType { self = [super init]; if (self) { - self.calendar = calendar; - self.weekdayTextType = textType; + _calendar = calendar; + _weekdayTextType = textType; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.calendar = calendar; - NSArray *weekdaySymbols = nil; switch (textType) { @@ -65,7 +56,7 @@ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendar weekdaySymbols = [dateFormatter standaloneWeekdaySymbols]; break; } - self.weekdaySymbols = weekdaySymbols; + _weekdaySymbols = weekdaySymbols; } return self; } From c40baa3a58dfb5940647d56ec76cda6252d05a04 Mon Sep 17 00:00:00 2001 From: "Andrew W. Donoho" Date: Fri, 29 Jun 2018 14:40:22 -0500 Subject: [PATCH 11/11] Update for Xcode v9.4. --- PDTSimpleCalendar/PDTSimpleCalendarViewController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 4f24950..51565ee 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -315,7 +315,7 @@ - (void)viewDidLoad #pragma mark - Rotation Handling -- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration +- (void) viewWillTransitionToSize: (CGSize) size withTransitionCoordinator: (id) coordinator { [self.collectionView.collectionViewLayout invalidateLayout]; }