diff --git a/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h b/PDTSimpleCalendar/PDTSimpleCalendarViewCell.h index 7e41c1c..64e7f9c 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 /** @@ -107,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 * @@ -116,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 36b01ef..ecbe494 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: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]; [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]; + } } } @@ -147,6 +169,16 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected [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 { @@ -161,6 +193,7 @@ - (void)prepareForReuse [super prepareForReuse]; _date = nil; _isToday = NO; + [self.noteLabel setHidden:YES]; [self.dayLabel setText:@""]; [self.dayLabel setBackgroundColor:[self circleDefaultColor]]; [self.dayLabel setTextColor:[self textDefaultColor]]; @@ -277,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.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..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:)]) { @@ -638,4 +639,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..933f0d3 100644 --- a/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m +++ b/PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m @@ -143,6 +143,27 @@ - (UIColor *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)con return [UIColor orangeColor]; } +- (NSString *)simpleCalendarViewController:(PDTSimpleCalendarViewController *)controller noteForDate:(NSDate *)date +{ + 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 //Add 3 custom dates, the 15th for the current & 2 next months.