diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 7e41c1c..9da3407 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -6,8 +6,10 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import -#import +@import UIKit; +NS_ASSUME_NONNULL_BEGIN + +extern const CGFloat kPDTSimpleCalendarCircleSize; @class PDTSimpleCalendarViewCell; @@ -34,7 +36,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,14 +47,14 @@ * * @return The circle desired color */ -- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; +- (nullable UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; @end /** * The `PDTSimpleCalendarViewCell` class displays a day in the calendar. */ -@interface PDTSimpleCalendarViewCell : UICollectionViewCell +@interface PDTSimpleCalendarViewCell : UICollectionViewCell /** * The delegate of the cell. @@ -65,7 +67,7 @@ /** * 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. @@ -114,7 +116,12 @@ * * @param calendar the calendar. */ -- (void)setDate:(NSDate*)date calendar:(NSCalendar*)calendar; +- (void)setDate:(nullable NSDate*)date calendar:(nullable NSCalendar*)calendar; + +/** + * Set the text for this cell. + */ +@property (nonatomic, strong) NSString *text; /** * Force the refresh of the colors for the circle and the text @@ -122,3 +129,4 @@ - (void)refreshCellColors; @end +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index 36b01ef..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 () @@ -21,6 +21,23 @@ @implementation PDTSimpleCalendarViewCell #pragma mark - Class Methods ++ (void)initialize { + if (self == [PDTSimpleCalendarViewCell class]) { + // Set the UIAppearance default values. + PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance]; + + proxy.circleDefaultColor = [UIColor whiteColor]; + proxy.circleTodayColor = [UIColor grayColor]; + proxy.circleSelectedColor = [UIColor redColor]; + + proxy.textDefaultColor = [UIColor blackColor]; + proxy.textTodayColor = [UIColor whiteColor]; + proxy.textSelectedColor = [UIColor whiteColor]; + proxy.textDisabledColor = [UIColor lightGrayColor]; + proxy.textDefaultFont = [UIFont systemFontOfSize: 17.0]; + } +} + + (NSString *)formatDate:(NSDate *)date withCalendar:(NSCalendar *)calendar { NSDateFormatter *dateFormatter = [self dateFormatter]; @@ -33,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; @@ -51,7 +67,6 @@ + (NSDateFormatter *)accessibilityDateFormatter { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterLongStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; - }); return dateFormatter; } @@ -66,34 +81,41 @@ + (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; - - [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]]; + [_dayLabel setTranslatesAutoresizingMaskIntoConstraints: NO]; + [_dayLabel setBackgroundColor:[UIColor clearColor]]; + _dayLabel.layer.cornerRadius = kPDTSimpleCalendarCircleSize/2; + _dayLabel.layer.masksToBounds = YES; - [self setCircleColor:NO selected:NO]; + [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; } +- (void) didMoveToWindow { + // Use the UIAppearance properties only after they been finalized by being + // inserted into a live window. + [super didMoveToWindow]; + + if (self.window) { + [self.dayLabel setFont: self.textDefaultFont]; + [self setCircleColor: self.isToday selected: self.selected]; + } +} + - (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar { NSString* day = @""; @@ -107,10 +129,23 @@ - (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar self.dayLabel.accessibilityLabel = accessibilityDay; } -- (void)setIsToday:(BOOL)isToday +@dynamic text; + +- (NSString *) text { + + return self.dayLabel.text; +} + +- (void) setText: (NSString *)text { + + self.dayLabel.text = text; + self.dayLabel.accessibilityLabel = text; +} + +- (void) setToday: (BOOL) today { - _isToday = isToday; - [self setCircleColor:isToday selected:self.selected]; + _today = today; + [self setCircleColor: today selected: self.selected]; } - (void)setSelected:(BOOL)selected @@ -137,144 +172,51 @@ - (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.dayLabel setText:@""]; - [self.dayLabel setBackgroundColor:[self circleDefaultColor]]; - [self.dayLabel setTextColor:[self textDefaultColor]]; -} - -#pragma mark - Circle Color Customization Methods - -- (UIColor *)circleDefaultColor -{ - if(_circleDefaultColor == nil) { - _circleDefaultColor = [[[self class] appearance] circleDefaultColor]; - } - - if(_circleDefaultColor != nil) { - return _circleDefaultColor; - } + self.date = nil; + self.today = NO; - return [UIColor whiteColor]; -} + // TODO: File a Doc bug, Thaddeus Cooper in DTS. Quinn & Larry, Kirby Turner. + PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance]; -- (UIColor *)circleTodayColor -{ - if(_circleTodayColor == nil) { - _circleTodayColor = [[[self class] appearance] circleTodayColor]; - } + if (_circleDefaultColor) { self.circleDefaultColor = proxy.circleDefaultColor; } + if (_circleTodayColor) { self.circleTodayColor = proxy.circleTodayColor; } + if (_circleSelectedColor) { self.circleSelectedColor = proxy.circleSelectedColor; } - if(_circleTodayColor != nil) { - return _circleTodayColor; - } + 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; } - return [UIColor grayColor]; -} - -- (UIColor *)circleSelectedColor -{ - if(_circleSelectedColor == nil) { - _circleSelectedColor = [[[self class] appearance] circleSelectedColor]; - } - - if(_circleSelectedColor != nil) { - return _circleSelectedColor; - } - - return [UIColor redColor]; + self.dayLabel.text = @""; + self.dayLabel.backgroundColor = proxy.circleDefaultColor; + self.dayLabel.textColor = proxy.textDefaultColor; } #pragma mark - Text Label Customizations Color -- (UIColor *)textDefaultColor -{ - if(_textDefaultColor == nil) { - _textDefaultColor = [[[self class] appearance] textDefaultColor]; - } - - if(_textDefaultColor != nil) { - return _textDefaultColor; - } - - return [UIColor blackColor]; -} - -- (UIColor *)textTodayColor -{ - if(_textTodayColor == nil) { - _textTodayColor = [[[self class] appearance] textTodayColor]; - } - - if(_textTodayColor != nil) { - return _textTodayColor; - } - - return [UIColor whiteColor]; -} - -- (UIColor *)textSelectedColor -{ - if(_textSelectedColor == nil) { - _textSelectedColor = [[[self class] appearance] textSelectedColor]; - } - - if(_textSelectedColor != nil) { - return _textSelectedColor; - } - - return [UIColor whiteColor]; -} - -- (UIColor *)textDisabledColor -{ - if(_textDisabledColor == nil) { - _textDisabledColor = [[[self class] appearance] textDisabledColor]; - } - - if(_textDisabledColor != nil) { - return _textDisabledColor; - } - - return [UIColor lightGrayColor]; -} - -#pragma mark - Text Label Customizations Font - -- (UIFont *)textDefaultFont +- (void) setTextDefaultColor: (UIColor *) textDefaultColor { - if(_textDefaultFont == nil) { - _textDefaultFont = [[[self class] appearance] textDefaultFont]; - } - - if (_textDefaultFont != nil) { - return _textDefaultFont; - } - - // default system font - return [UIFont systemFontOfSize:17.0]; + _textDefaultColor = textDefaultColor ?: [[[self class] appearance] textDefaultColor]; + self.dayLabel.textColor = _textDefaultColor; } @end diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h index 661e056..2ce59ae 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h @@ -6,9 +6,10 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import +@import UIKit; #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..51565ee 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 @@ -308,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]; } @@ -375,28 +382,36 @@ - (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 setText: 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]; } - - 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) { + cell.textDefaultColor = cell.textDisabledColor; [cell refreshCellColors]; } @@ -444,8 +459,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 +475,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(kPDTSimpleCalendarCircleSize * 1.2))); } #pragma mark - UIScrollViewDelegate @@ -495,7 +517,6 @@ - (void)hideOverlayView }]; } -#pragma mark - #pragma mark - Calendar calculations - (NSDate *)clampDate:(NSDate *)date toComponents:(NSUInteger)unitFlags @@ -619,7 +640,7 @@ - (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColo } if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:circleColorForDate:)]) { - return [self.delegate simpleCalendarViewController:self circleColorForDate:date]; + return [self.delegate simpleCalendarViewController:self circleColorForDate:date] ?: cell.circleDefaultColor; } return nil; @@ -628,11 +649,11 @@ - (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColo - (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorForDate:(NSDate *)date { if (![self isEnabledDate:date]) { - return cell.textDisabledColor; + return cell.textDefaultColor; } if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:textColorForDate:)]) { - return [self.delegate simpleCalendarViewController:self textColorForDate:date]; + return [self.delegate simpleCalendarViewController:self textColorForDate:date] ?: cell.textDefaultColor; } return nil; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h b/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h index 61a3c5b..98e36aa 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewFlowLayout.h @@ -6,7 +6,8 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import +@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 377304b..ae4299b 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.h @@ -6,12 +6,13 @@ // Copyright (c) 2013 Producteev. All rights reserved. // -#import +@import UIKit; +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 @@ -36,3 +37,4 @@ @property (nonatomic, strong) UIColor *separatorColor UI_APPEARANCE_SELECTOR; @end +NS_ASSUME_NONNULL_END diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m index 92bf498..c2484fb 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewHeader.m @@ -8,31 +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 -- (id)initWithFrame:(CGRect)frame ++ (void)initialize { + 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: kPDTSimpleCalendarHeaderTextSize]]; + } +} + +- (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]]; @@ -44,47 +57,19 @@ - (id)initWithFrame:(CGRect)frame return self; } +- (void) didMoveToWindow { -#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]; -} + [super didMoveToWindow]; -- (UIColor *)separatorColor -{ - if(_separatorColor == nil) { - _separatorColor = [[[self class] appearance] separatorColor]; - } + if (self.window) { + // Use the UIAppearance properties only after they been finalized by being + // inserted into a live window. + self.separatorView.backgroundColor = self.separatorColor; - if(_separatorColor != nil) { - return _separatorColor; + self.titleLabel.font = self.textFont; + self.titleLabel.textColor = self.textColor; + self.titleLabel.backgroundColor = UIColor.clearColor; } - - return [UIColor lightGrayColor]; } - @end diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h index 03f832a..8679108 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.h @@ -6,7 +6,8 @@ // Copyright (c) 2015 MorningCall. All rights reserved. // -#import +@import UIKit; +NS_ASSUME_NONNULL_BEGIN extern const CGFloat PDTSimpleCalendarWeekdayHeaderSize; extern const CGFloat PDTSimpleCalendarWeekdayHeaderHeight; @@ -17,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; @@ -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 2d1a4d4..22b508e 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewWeekdayHeader.m @@ -11,26 +11,40 @@ 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 -/* -// Only override drawRect: if you perform custom drawing. -// An empty implementation adversely affects performance during animation. -- (void)drawRect:(CGRect)rect { - // Drawing code ++ (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]]; + } } -*/ - (id)initWithCalendar:(NSCalendar *)calendar weekdayTextType:(PDTSimpleCalendarViewWeekdayTextType)textType { self = [super init]; if (self) { - self.backgroundColor = self.headerBackgroundColor; + _calendar = calendar; + _weekdayTextType = textType; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.calendar = calendar; NSArray *weekdaySymbols = nil; - + switch (textType) { case PDTSimpleCalendarViewWeekdayTextTypeVeryShort: weekdaySymbols = [dateFormatter veryShortWeekdaySymbols]; @@ -42,27 +56,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++) { + _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]; @@ -70,64 +94,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; -} - -- (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