Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions PDTSimpleCalendar/PDTSimpleCalendarViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*/
Expand Down
46 changes: 46 additions & 0 deletions PDTSimpleCalendar/PDTSimpleCalendarViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

@interface PDTSimpleCalendarViewCell ()

@property (nonatomic, strong) UILabel *noteLabel;
@property (nonatomic, strong) UILabel *dayLabel;
@property (nonatomic, strong) NSDate *date;

Expand Down Expand Up @@ -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]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allow customization of this font using UIAppearance? That could be handy.

[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]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we rely on the frame size rather than the PDTSimpleCalendarSize? I think it will be more robust.


_dayLabel = [[UILabel alloc] init];
[self.dayLabel setFont:[self textDefaultFont]];
[self.dayLabel setTextAlignment:NSTextAlignmentCenter];
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need the "double" delegate hoop. PDTSimpleCalendar can have a simple setNote:(NSString *)noteText method and it will work. Don't forget to reset it in prepareForReuse. For even better performance, when the text is nil or empty, we could completely hide the noteLabel view.

note = [self.delegate simpleCalendarViewCell:self noteForDate:self.date];
}
}
}

Expand All @@ -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
{
Expand All @@ -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]];
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions PDTSimpleCalendar/PDTSimpleCalendarViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 14 additions & 0 deletions PDTSimpleCalendar/PDTSimpleCalendarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:)]) {
Expand Down Expand Up @@ -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
21 changes: 21 additions & 0 deletions PDTSimpleCalendarDemo/PDTSimpleCalendarDemo/PDTAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down