From f46782ea8b01d6cfb62853be847d509b25e12396 Mon Sep 17 00:00:00 2001 From: Yuwen Yan Date: Sun, 4 Oct 2015 21:05:08 +0800 Subject: [PATCH 1/3] add note label for each day --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 11 +++++++++ PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 24 +++++++++++++++++++ .../PDTSimpleCalendarViewController.h | 8 +++++++ .../PDTSimpleCalendarViewController.m | 13 ++++++++++ .../PDTSimpleCalendarDemo/PDTAppDelegate.m | 7 +++++- 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 7e41c1c..38992b6 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -47,6 +47,17 @@ */ - (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date; +/** + * Asks the delegate for the note for a specific date. + * Will be called only if the delegate returns YES for `- (BOOL)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell shouldUseCustomColorsForDate:(NSDate *)date;` + * + * @param cell the current cell + * @param date the date associated with the cell + * + * @return The note + */ +- (NSString *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell noteForDate:(NSDate *)date; + @end /** diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index 36b01ef..e1a6294 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -12,6 +12,7 @@ @interface PDTSimpleCalendarViewCell () +@property (nonatomic, strong) UILabel *noteLabel; @property (nonatomic, strong) UILabel *dayLabel; @property (nonatomic, strong) NSDate *date; @@ -72,6 +73,22 @@ - (id)initWithFrame:(CGRect)frame if (self) { _date = nil; _isToday = NO; + + _noteLabel = [[UILabel alloc] init]; + [self.noteLabel setFont:[UIFont systemFontOfSize:10.0]]; + [self.noteLabel setTextAlignment:NSTextAlignmentCenter]; + [self.contentView addSubview:self.noteLabel]; + + //Add the Constraints + [self.noteLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; + [self.noteLabel setBackgroundColor:[UIColor clearColor]]; + self.noteLabel.layer.masksToBounds = YES; + + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.6]]; + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.6]]; + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.9 constant:0.0]]; + _dayLabel = [[UILabel alloc] init]; [self.dayLabel setFont:[self textDefaultFont]]; [self.dayLabel setTextAlignment:NSTextAlignmentCenter]; @@ -124,6 +141,7 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected { UIColor *circleColor = (today) ? [self circleTodayColor] : [self circleDefaultColor]; UIColor *labelColor = (today) ? [self textTodayColor] : [self textDefaultColor]; + NSString *note = @""; if (self.date && self.delegate) { if ([self.delegate respondsToSelector:@selector(simpleCalendarViewCell:shouldUseCustomColorsForDate:)] && [self.delegate simpleCalendarViewCell:self shouldUseCustomColorsForDate:self.date]) { @@ -135,6 +153,10 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected if ([self.delegate respondsToSelector:@selector(simpleCalendarViewCell:circleColorForDate:)] && [self.delegate simpleCalendarViewCell:self circleColorForDate:self.date]) { circleColor = [self.delegate simpleCalendarViewCell:self circleColorForDate:self.date]; } + + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewCell:noteForDate:)] && [self.delegate simpleCalendarViewCell:self noteForDate:self.date]) { + note = [self.delegate simpleCalendarViewCell:self noteForDate:self.date]; + } } } @@ -143,6 +165,7 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected labelColor = [self textSelectedColor]; } + [self.noteLabel setText:note]; [self.dayLabel setBackgroundColor:circleColor]; [self.dayLabel setTextColor:labelColor]; } @@ -161,6 +184,7 @@ - (void)prepareForReuse [super prepareForReuse]; _date = nil; _isToday = NO; + [self.noteLabel setText:@""]; [self.dayLabel setText:@""]; [self.dayLabel setBackgroundColor:[self circleDefaultColor]]; [self.dayLabel setTextColor:[self textDefaultColor]]; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h index 661e056..29b031b 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.h @@ -154,4 +154,12 @@ */ - (UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller textColorForDate:(NSDate *)date; +/** + * Asks the delegate for the note for a custom added date + * + * @param controller the calendarView Controller + * @param date the date (Midnight GMT) + */ +- (NSString *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller noteForDate:(NSDate *)date; + @end; diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 658e711..8fd8761 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -638,4 +638,17 @@ - (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell textColorF return nil; } +- (NSString *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell noteForDate:(NSDate *)date +{ + if (![self isEnabledDate:date]) { + return @""; + } + + if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:noteForDate:)]) { + return [self.delegate simpleCalendarViewController:self noteForDate:date]; + } + + return nil; +} + @end diff --git a/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m b/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m index 1e1644f..e094b9d 100644 --- a/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m +++ b/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m @@ -143,6 +143,11 @@ - (UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)con return [UIColor orangeColor]; } +- (NSString *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller noteForDate:(NSDate *)date +{ + return @"note"; +} + #pragma mark - Private //Add 3 custom dates, the 15th for the current & 2 next months. @@ -155,7 +160,7 @@ - (void)initCustomDates addOneMonthComponents.month =1; NSDate *date2 = [[NSCalendar currentCalendar] dateByAddingComponents:addOneMonthComponents toDate:date1 options:0]; NSDate *date3 = [[NSCalendar currentCalendar] dateByAddingComponents:addOneMonthComponents toDate:date2 options:0]; - self.customDates = @[date1, date2, date3]; + self.customDates = @[date1, date2, date3, [NSDate date]]; } From 9f6ff8fcd3fffca993e6ad612611d648bb605e1c Mon Sep 17 00:00:00 2001 From: Yuwen Yan Date: Sat, 24 Oct 2015 20:09:44 +0800 Subject: [PATCH 2/3] adjust note label position --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index e1a6294..04718df 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -85,7 +85,7 @@ - (id)initWithFrame:(CGRect)frame self.noteLabel.layer.masksToBounds = YES; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.6]]; + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.8]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.6]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.9 constant:0.0]]; From f64319ffc5579b634a4b2c54b9131683c79a697e Mon Sep 17 00:00:00 2001 From: Yuwen Yan Date: Sat, 7 Nov 2015 10:46:02 +0800 Subject: [PATCH 3/3] fix issues in PR #81 --- PDTSimpleCalendar/PDTSimpleCalendarViewCell.h | 12 ++++++++ PDTSimpleCalendar/PDTSimpleCalendarViewCell.m | 30 ++++++++++++++++--- .../PDTSimpleCalendarViewController.m | 1 + .../PDTSimpleCalendarDemo/PDTAppDelegate.m | 20 +++++++++++-- 4 files changed, 57 insertions(+), 6 deletions(-) diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 38992b6..64e7f9c 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h @@ -118,6 +118,11 @@ */ @property (nonatomic, strong) UIFont *textDefaultFont UI_APPEARANCE_SELECTOR; +/** + * Customize the note's font using UIAppearance. + */ +@property (nonatomic, strong) UIFont *noteFont UI_APPEARANCE_SELECTOR; + /** * Set the date for this cell * @@ -127,6 +132,13 @@ */ - (void)setDate:(NSDate*)date calendar:(NSCalendar*)calendar; +/** + * Set note for this cell + * + * @param noteText the note. + */ +- (void)setNote:(NSString *)noteText; + /** * Force the refresh of the colors for the circle and the text */ diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m index 04718df..ecbe494 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.m @@ -85,8 +85,8 @@ - (id)initWithFrame:(CGRect)frame self.noteLabel.layer.masksToBounds = YES; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.8]]; - [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize * 0.6]]; + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:2.0 constant:0.0]]; + [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeHeight multiplier:0.3 constant:0.0]]; [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.noteLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.9 constant:0.0]]; _dayLabel = [[UILabel alloc] init]; @@ -165,11 +165,20 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected labelColor = [self textSelectedColor]; } - [self.noteLabel setText:note]; [self.dayLabel setBackgroundColor:circleColor]; [self.dayLabel setTextColor:labelColor]; } +- (void)setNote:(NSString *)noteText +{ + if (noteText == nil || noteText.length == 0) { + [self.noteLabel setHidden:YES]; + } else { + [self.noteLabel setHidden:NO]; + [self.noteLabel setText:noteText]; + } +} + - (void)refreshCellColors { @@ -184,7 +193,7 @@ - (void)prepareForReuse [super prepareForReuse]; _date = nil; _isToday = NO; - [self.noteLabel setText:@""]; + [self.noteLabel setHidden:YES]; [self.dayLabel setText:@""]; [self.dayLabel setBackgroundColor:[self circleDefaultColor]]; [self.dayLabel setTextColor:[self textDefaultColor]]; @@ -301,4 +310,17 @@ - (UIFont *)textDefaultFont return [UIFont systemFontOfSize:17.0]; } +- (UIFont *)noteFont +{ + if(_noteFont == nil) { + _noteFont = [[[self class] appearance] noteFont]; + } + + if (_noteFont != nil) { + return _noteFont; + } + + return [UIFont systemFontOfSize:10.0]; +} + @end diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m index 8fd8761..c136f13 100644 --- a/PDTSimpleCalendar/PDTSimpleCalendarViewController.m +++ b/PDTSimpleCalendar/PDTSimpleCalendarViewController.m @@ -376,6 +376,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell isSelected = ([self isSelectedDate:cellDate] && (indexPath.section == [self sectionForDate:cellDate])); isToday = [self isTodayDate:cellDate]; [cell setDate:cellDate calendar:self.calendar]; + [cell setNote:[self.delegate simpleCalendarViewController:self noteForDate:cellDate]]; //Ask the delegate if this date should have specific colors. if ([self.delegate respondsToSelector:@selector(simpleCalendarViewController:shouldUseCustomColorsForDate:)]) { diff --git a/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m b/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m index e094b9d..933f0d3 100644 --- a/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m +++ b/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m @@ -145,7 +145,23 @@ - (UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)con - (NSString *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller noteForDate:(NSDate *)date { - return @"note"; + static NSString *todayStr = nil; + static NSDateFormatter *formatter = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + formatter = [[NSDateFormatter alloc] init]; + [formatter setDateFormat:@"yyyy-MM-dd"]; + + todayStr = [formatter stringFromDate:[NSDate date]]; + }); + + NSString *dateStr = [formatter stringFromDate:date]; + + if ([dateStr isEqualToString:todayStr]) { + return @"today"; + } else { + return nil; + } } #pragma mark - Private @@ -160,7 +176,7 @@ - (void)initCustomDates addOneMonthComponents.month =1; NSDate *date2 = [[NSCalendar currentCalendar] dateByAddingComponents:addOneMonthComponents toDate:date1 options:0]; NSDate *date3 = [[NSCalendar currentCalendar] dateByAddingComponents:addOneMonthComponents toDate:date2 options:0]; - self.customDates = @[date1, date2, date3, [NSDate date]]; + self.customDates = @[date1, date2, date3]; }