Skip to content

Commit

Permalink
allow different textColors for enabled cells
Browse files Browse the repository at this point in the history
  • Loading branch information
min committed Jul 28, 2013
1 parent b380734 commit 3594adb
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
7 changes: 7 additions & 0 deletions MNCalendarView/MNCalendarHeaderView.m
Expand Up @@ -12,4 +12,11 @@

@implementation MNCalendarHeaderView

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = UIColor.whiteColor;
}
return self;
}

@end
23 changes: 13 additions & 10 deletions MNCalendarView/MNCalendarView.m
Expand Up @@ -33,10 +33,10 @@ @implementation MNCalendarView

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.calendar = [NSCalendar currentCalendar];
self.fromDate = [[NSDate date] mn_beginningOfDay:self.calendar];
self.calendar = NSCalendar.currentCalendar;
self.fromDate = [NSDate.date mn_beginningOfDay:self.calendar];
self.toDate = [self.fromDate dateByAddingTimeInterval:MN_YEAR * 4];
self.separatorColor = [UIColor grayColor];
self.separatorColor = UIColor.lightGrayColor;

self.daysInWeek = 7;

Expand Down Expand Up @@ -128,9 +128,9 @@ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
MNCalendarHeaderView *headerView =
[collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:MNCalendarHeaderViewIdentifier
forIndexPath:indexPath];
[collectionView dequeueReusableSupplementaryViewOfKind:kind
withReuseIdentifier:MNCalendarHeaderViewIdentifier
forIndexPath:indexPath];
return headerView;
}

Expand All @@ -155,10 +155,13 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView

cell.separatorColor = self.separatorColor;

NSDate *monthDate =
[self firstVisibleDateOfMonth:self.monthDates[indexPath.section]];

cell.date = [monthDate dateByAddingTimeInterval:indexPath.item * MN_DAY];
NSDate *monthDate = self.monthDates[indexPath.section];
NSDate *firstDateInMonth =
[self firstVisibleDateOfMonth:monthDate];

[cell setDate:[firstDateInMonth dateByAddingTimeInterval:indexPath.item * MN_DAY]
month:monthDate
calendar:self.calendar];

return cell;
}
Expand Down
10 changes: 9 additions & 1 deletion MNCalendarView/MNCalendarViewCell.h
Expand Up @@ -12,8 +12,16 @@ extern NSString *const MNCalendarViewCellIdentifier;

@interface MNCalendarViewCell : UICollectionViewCell

@property(nonatomic,strong) NSDate *date;
@property(nonatomic,strong,readonly) NSDate *date;
@property(nonatomic,strong,readonly) NSDate *month;
@property(nonatomic,strong,readonly) NSCalendar *calendar;

@property(nonatomic,assign,getter = isEnabled) BOOL enabled;

@property(nonatomic,strong) UIColor *separatorColor;

- (void)setDate:(NSDate *)date
month:(NSDate *)month
calendar:(NSCalendar *)calendar;

@end
36 changes: 32 additions & 4 deletions MNCalendarView/MNCalendarViewCell.m
Expand Up @@ -22,7 +22,11 @@ void MNContextDrawLine(CGContextRef c, CGPoint start, CGPoint end, CGColorRef co

@interface MNCalendarViewCell()

@property(nonatomic,readwrite) UILabel *titleLabel;
@property(nonatomic,strong,readwrite) UILabel *titleLabel;

@property(nonatomic,strong,readwrite) NSDate *date;
@property(nonatomic,strong,readwrite) NSDate *month;
@property(nonatomic,strong,readwrite) NSCalendar *calendar;

@end

Expand All @@ -35,7 +39,8 @@ - (id)initWithFrame:(CGRect)frame {

self.titleLabel = [[UILabel alloc] initWithFrame:self.bounds];
self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.titleLabel.font = [UIFont boldSystemFontOfSize:12.f];
self.titleLabel.font = [UIFont systemFontOfSize:14.f];
self.titleLabel.textColor = [UIColor darkTextColor];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.userInteractionEnabled = NO;
self.titleLabel.backgroundColor = [UIColor clearColor];
Expand All @@ -56,12 +61,35 @@ - (void)layoutSubviews {
self.selectedBackgroundView.frame = self.bounds;
}

- (void)setDate:(NSDate *)date {
_date = date;
- (void)setDate:(NSDate *)date
month:(NSDate *)month
calendar:(NSCalendar *)calendar {

self.date = date;
self.month = month;
self.calendar = calendar;

NSDateComponents *components =
[self.calendar components:NSMonthCalendarUnit|NSDayCalendarUnit
fromDate:self.date];

NSDateComponents *monthComponents =
[self.calendar components:NSMonthCalendarUnit
fromDate:self.month];

self.titleLabel.text = [NSString stringWithFormat:@"%d", components.day];
self.enabled = monthComponents.month == components.month;

[self setNeedsDisplay];
}

- (void)setEnabled:(BOOL)enabled {
_enabled = enabled;

self.titleLabel.textColor =
self.enabled ? UIColor.darkTextColor : UIColor.lightGrayColor;
}

- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();

Expand Down

0 comments on commit 3594adb

Please sign in to comment.