Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine UIAppearance behavior #102

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
22 changes: 15 additions & 7 deletions PDTSimpleCalendar/PDTSimpleCalendarViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
// Copyright (c) 2013 Producteev. All rights reserved.
//

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

extern const CGFloat kPDTSimpleCalendarCircleSize;

@class PDTSimpleCalendarViewCell;

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

@end

/**
* The `PDTSimpleCalendarViewCell` class displays a day in the calendar.
*/
@interface PDTSimpleCalendarViewCell : UICollectionViewCell
@interface PDTSimpleCalendarViewCell : UICollectionViewCell <UIAppearanceContainer>

/**
* The delegate of the cell.
Expand All @@ -65,7 +67,7 @@
/**
* Define if the cell is today in the calendar.
*/
@property (nonatomic, assign) BOOL isToday;
@property (nonatomic, assign, getter=isToday) BOOL today;

/**
* Customize the circle behind the day's number color using UIAppearance.
Expand Down Expand Up @@ -114,11 +116,17 @@
*
* @param calendar the calendar.
*/
- (void)setDate:(NSDate*)date calendar:(NSCalendar*)calendar;
- (void)setDate:(nullable NSDate*)date calendar:(nullable NSCalendar*)calendar;

/**
* Set the text for this cell.
*/
@property (nonatomic, strong) NSString *text;

/**
* Force the refresh of the colors for the circle and the text
*/
- (void)refreshCellColors;

@end
NS_ASSUME_NONNULL_END
208 changes: 75 additions & 133 deletions PDTSimpleCalendar/PDTSimpleCalendarViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "PDTSimpleCalendarViewCell.h"

const CGFloat PDTSimpleCalendarCircleSize = 32.0f;
const CGFloat kPDTSimpleCalendarCircleSize = 32.0f;

@interface PDTSimpleCalendarViewCell ()

Expand All @@ -21,6 +21,23 @@ @implementation PDTSimpleCalendarViewCell

#pragma mark - Class Methods

+ (void)initialize {
if (self == [PDTSimpleCalendarViewCell class]) {
// Set the UIAppearance default values.
PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance];

proxy.circleDefaultColor = [UIColor whiteColor];
proxy.circleTodayColor = [UIColor grayColor];
proxy.circleSelectedColor = [UIColor redColor];

proxy.textDefaultColor = [UIColor blackColor];
proxy.textTodayColor = [UIColor whiteColor];
proxy.textSelectedColor = [UIColor whiteColor];
proxy.textDisabledColor = [UIColor lightGrayColor];
proxy.textDefaultFont = [UIFont systemFontOfSize: 17.0];
}
}

+ (NSString *)formatDate:(NSDate *)date withCalendar:(NSCalendar *)calendar
{
NSDateFormatter *dateFormatter = [self dateFormatter];
Expand All @@ -33,7 +50,6 @@ + (NSString *)formatAccessibilityDate:(NSDate *)date withCalendar:(NSCalendar *)
return [PDTSimpleCalendarViewCell stringFromDate:date withDateFormatter:dateFormatter withCalendar:calendar];
}


+ (NSDateFormatter *)dateFormatter {
static NSDateFormatter *dateFormatter;
static dispatch_once_t onceToken;
Expand All @@ -51,7 +67,6 @@ + (NSDateFormatter *)accessibilityDateFormatter {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

});
return dateFormatter;
}
Expand All @@ -66,34 +81,41 @@ + (NSString *)stringFromDate:(NSDate *)date withDateFormatter:(NSDateFormatter *

#pragma mark - Instance Methods

- (id)initWithFrame:(CGRect)frame
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_date = nil;
_isToday = NO;
_today = NO;
_dayLabel = [[UILabel alloc] init];
[self.dayLabel setFont:[self textDefaultFont]];
[self.dayLabel setTextAlignment:NSTextAlignmentCenter];
[self.contentView addSubview:self.dayLabel];
[_dayLabel setTextAlignment: NSTextAlignmentCenter];
[self.contentView addSubview: _dayLabel];

//Add the Constraints
[self.dayLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.dayLabel setBackgroundColor:[UIColor clearColor]];
self.dayLabel.layer.cornerRadius = PDTSimpleCalendarCircleSize/2;
self.dayLabel.layer.masksToBounds = YES;

[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.dayLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:PDTSimpleCalendarCircleSize]];
[_dayLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
[_dayLabel setBackgroundColor:[UIColor clearColor]];
_dayLabel.layer.cornerRadius = kPDTSimpleCalendarCircleSize/2;
_dayLabel.layer.masksToBounds = YES;

[self setCircleColor:NO selected:NO];
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeCenterX relatedBy: NSLayoutRelationEqual toItem: self.contentView attribute: NSLayoutAttributeCenterX multiplier: 1.0 constant: 0.0]];
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeCenterY relatedBy: NSLayoutRelationEqual toItem: self.contentView attribute: NSLayoutAttributeCenterY multiplier: 1.0 constant: 0.0]];
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: kPDTSimpleCalendarCircleSize]];
[self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _dayLabel attribute: NSLayoutAttributeWidth relatedBy: NSLayoutRelationEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1.0 constant: kPDTSimpleCalendarCircleSize]];
}

return self;
}

- (void) didMoveToWindow {
// Use the UIAppearance properties only after they been finalized by being
// inserted into a live window.
[super didMoveToWindow];

if (self.window) {
[self.dayLabel setFont: self.textDefaultFont];
[self setCircleColor: self.isToday selected: self.selected];
}
}

- (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar
{
NSString* day = @"";
Expand All @@ -107,10 +129,23 @@ - (void)setDate:(NSDate *)date calendar:(NSCalendar *)calendar
self.dayLabel.accessibilityLabel = accessibilityDay;
}

- (void)setIsToday:(BOOL)isToday
@dynamic text;

- (NSString *) text {

return self.dayLabel.text;
}

- (void) setText: (NSString *)text {

self.dayLabel.text = text;
self.dayLabel.accessibilityLabel = text;
}

- (void) setToday: (BOOL) today
{
_isToday = isToday;
[self setCircleColor:isToday selected:self.selected];
_today = today;
[self setCircleColor: today selected: self.selected];
}

- (void)setSelected:(BOOL)selected
Expand All @@ -137,144 +172,51 @@ - (void)setCircleColor:(BOOL)today selected:(BOOL)selected
}
}
}

if (selected) {
circleColor = [self circleSelectedColor];
labelColor = [self textSelectedColor];
}

[self.dayLabel setBackgroundColor:circleColor];
[self.dayLabel setTextColor:labelColor];
}


- (void)refreshCellColors
{
[self setCircleColor:self.isToday selected:self.isSelected];
}


#pragma mark - Prepare for Reuse

- (void)prepareForReuse
{
[super prepareForReuse];
_date = nil;
_isToday = NO;
[self.dayLabel setText:@""];
[self.dayLabel setBackgroundColor:[self circleDefaultColor]];
[self.dayLabel setTextColor:[self textDefaultColor]];
}

#pragma mark - Circle Color Customization Methods

- (UIColor *)circleDefaultColor
{
if(_circleDefaultColor == nil) {
_circleDefaultColor = [[[self class] appearance] circleDefaultColor];
}

if(_circleDefaultColor != nil) {
return _circleDefaultColor;
}
self.date = nil;
self.today = NO;

return [UIColor whiteColor];
}
// TODO: File a Doc bug, Thaddeus Cooper in DTS. Quinn & Larry, Kirby Turner.
PDTSimpleCalendarViewCell *proxy = [PDTSimpleCalendarViewCell appearance];

- (UIColor *)circleTodayColor
{
if(_circleTodayColor == nil) {
_circleTodayColor = [[[self class] appearance] circleTodayColor];
}
if (_circleDefaultColor) { self.circleDefaultColor = proxy.circleDefaultColor; }
if (_circleTodayColor) { self.circleTodayColor = proxy.circleTodayColor; }
if (_circleSelectedColor) { self.circleSelectedColor = proxy.circleSelectedColor; }

if(_circleTodayColor != nil) {
return _circleTodayColor;
}
if (_textDefaultColor) { self.textDefaultColor = proxy.textDefaultColor; }
if (_textTodayColor) { self.textTodayColor = proxy.textTodayColor; }
if (_textSelectedColor) { self.textSelectedColor = proxy.textSelectedColor; }
if (_textDisabledColor) { self.textDisabledColor = proxy.textDisabledColor; }
if (_textDefaultFont) { self.textDefaultFont = proxy.textDefaultFont; }

return [UIColor grayColor];
}

- (UIColor *)circleSelectedColor
{
if(_circleSelectedColor == nil) {
_circleSelectedColor = [[[self class] appearance] circleSelectedColor];
}

if(_circleSelectedColor != nil) {
return _circleSelectedColor;
}

return [UIColor redColor];
self.dayLabel.text = @"";
self.dayLabel.backgroundColor = proxy.circleDefaultColor;
self.dayLabel.textColor = proxy.textDefaultColor;
}

#pragma mark - Text Label Customizations Color

- (UIColor *)textDefaultColor
{
if(_textDefaultColor == nil) {
_textDefaultColor = [[[self class] appearance] textDefaultColor];
}

if(_textDefaultColor != nil) {
return _textDefaultColor;
}

return [UIColor blackColor];
}

- (UIColor *)textTodayColor
{
if(_textTodayColor == nil) {
_textTodayColor = [[[self class] appearance] textTodayColor];
}

if(_textTodayColor != nil) {
return _textTodayColor;
}

return [UIColor whiteColor];
}

- (UIColor *)textSelectedColor
{
if(_textSelectedColor == nil) {
_textSelectedColor = [[[self class] appearance] textSelectedColor];
}

if(_textSelectedColor != nil) {
return _textSelectedColor;
}

return [UIColor whiteColor];
}

- (UIColor *)textDisabledColor
{
if(_textDisabledColor == nil) {
_textDisabledColor = [[[self class] appearance] textDisabledColor];
}

if(_textDisabledColor != nil) {
return _textDisabledColor;
}

return [UIColor lightGrayColor];
}

#pragma mark - Text Label Customizations Font

- (UIFont *)textDefaultFont
- (void) setTextDefaultColor: (UIColor *) textDefaultColor
{
if(_textDefaultFont == nil) {
_textDefaultFont = [[[self class] appearance] textDefaultFont];
}

if (_textDefaultFont != nil) {
return _textDefaultFont;
}

// default system font
return [UIFont systemFontOfSize:17.0];
_textDefaultColor = textDefaultColor ?: [[[self class] appearance] textDefaultColor];
self.dayLabel.textColor = _textDefaultColor;
}

@end