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
19 changes: 15 additions & 4 deletions PDTSimpleCalendar/PDTSimpleCalendarViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN

extern const CGFloat PDTSimpleCalendarCircleSize;

@class PDTSimpleCalendarViewCell;

Expand All @@ -34,7 +37,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.
Expand All @@ -45,7 +48,7 @@
*
* @return The circle desired color
*/
- (UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date;
- (nullable UIColor *)simpleCalendarViewCell:(PDTSimpleCalendarViewCell *)cell circleColorForDate:(NSDate *)date;

@end

Expand Down Expand Up @@ -85,7 +88,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.
Expand Down Expand Up @@ -114,11 +117,19 @@
*
* @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
*/
- (void)refreshCellColors;

@end
NS_ASSUME_NONNULL_END
16 changes: 16 additions & 0 deletions PDTSimpleCalendar/PDTSimpleCalendarViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ @interface PDTSimpleCalendarViewCell ()

@implementation PDTSimpleCalendarViewCell

@synthesize textDefaultColor = _textDefaultColor;

#pragma mark - Class Methods

+ (NSString *)formatDate:(NSDate *)date withCalendar:(NSCalendar *)calendar
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]];
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 14 additions & 4 deletions PDTSimpleCalendar/PDTSimpleCalendarViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <UIKit/UIKit.h>

#import "PDTSimpleCalendarViewWeekdayHeader.h"
NS_ASSUME_NONNULL_BEGIN

@protocol PDTSimpleCalendarViewDelegate;

Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -80,7 +81,7 @@
*
* @see PDTSimpleCalendarViewDelegate
*/
@property (nonatomic, weak) id<PDTSimpleCalendarViewDelegate> delegate;
@property (nullable, nonatomic, weak) id<PDTSimpleCalendarViewDelegate> delegate;


/**
Expand Down Expand Up @@ -144,14 +145,23 @@
* @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
*
* @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
56 changes: 42 additions & 14 deletions PDTSimpleCalendar/PDTSimpleCalendarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
Expand All @@ -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];
}

Expand Down Expand Up @@ -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
Expand All @@ -454,7 +481,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
Expand Down Expand Up @@ -612,27 +640,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;
Expand Down