From ecd6850e2152e69a998913f3087159f48fc0106e Mon Sep 17 00:00:00 2001 From: Wenchao Ding Date: Wed, 11 Mar 2015 18:32:56 +0800 Subject: [PATCH] Create branch delete-attribute-string Create branch delete-attribute-string --- Example/FSCalendar/ViewController.m | 5 +- Example/Pods/Pods.xcodeproj/project.pbxproj | 6 + Pod/Classes/CALayer+FSExtension.h | 21 +++ Pod/Classes/CALayer+FSExtension.m | 73 ++++++++ Pod/Classes/FSCalendar.m | 197 ++++++++++++-------- Pod/Classes/FSCalendarUnit.h | 15 +- Pod/Classes/FSCalendarUnit.m | 128 +++---------- 7 files changed, 256 insertions(+), 189 deletions(-) create mode 100644 Pod/Classes/CALayer+FSExtension.h create mode 100644 Pod/Classes/CALayer+FSExtension.m diff --git a/Example/FSCalendar/ViewController.m b/Example/FSCalendar/ViewController.m index bd377da..eb666d4 100644 --- a/Example/FSCalendar/ViewController.m +++ b/Example/FSCalendar/ViewController.m @@ -15,7 +15,6 @@ #define kBlue [UIColor colorWithRed:31/255.0 green:119/255.0 blue:219/255.0 alpha:1.0] #define kBlueText [UIColor colorWithRed:14/255.0 green:69/255.0 blue:221/255.0 alpha:1.0] - @implementation ViewController - (void)viewDidLoad @@ -32,7 +31,9 @@ - (NSString *)calendar:(FSCalendar *)calendarView subtitleForDate:(NSDate *)date { - return _subtitle ? [[SSLunarDate alloc] initWithDate:date].dayString : nil; +// return _subtitle ? [[SSLunarDate alloc] initWithDate:date].dayString : nil; + return @"123"; +// return nil; } - (BOOL)calendar:(FSCalendar *)calendarView hasEventForDate:(NSDate *)date diff --git a/Example/Pods/Pods.xcodeproj/project.pbxproj b/Example/Pods/Pods.xcodeproj/project.pbxproj index b46a31a..5d47768 100644 --- a/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -28,6 +28,7 @@ 4FD6E939AE65BB0AE03EB217 /* SSHolidayUS.h in Headers */ = {isa = PBXBuildFile; fileRef = 1754C9EE0857D9A893A33EFC /* SSHolidayUS.h */; }; 5029DA571AA8111F00712B83 /* UIScrollView+FSExtension.h in Headers */ = {isa = PBXBuildFile; fileRef = 5029DA551AA8111F00712B83 /* UIScrollView+FSExtension.h */; }; 5029DA581AA8111F00712B83 /* UIScrollView+FSExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 5029DA561AA8111F00712B83 /* UIScrollView+FSExtension.m */; }; + 5056E8821AB04B1D00550681 /* CALayer+FSExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = 5056E8811AB04B1D00550681 /* CALayer+FSExtension.m */; }; 54B9A49CD681332E0AC96ACB /* SSHolidayManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 994C4E5A9ADBA9E195ED3178 /* SSHolidayManager.m */; }; 5BCDC7864F306CA125751EF5 /* FSCalendar.h in Headers */ = {isa = PBXBuildFile; fileRef = 1CF2B5A08202EC5C784DA2EE /* FSCalendar.h */; }; 60934D965DDDD69787765519 /* SSHolidayUK.m in Sources */ = {isa = PBXBuildFile; fileRef = 12DE6656C02C82877B52AE19 /* SSHolidayUK.m */; }; @@ -125,6 +126,8 @@ 4E33EC3A0C38A7AC4F8FBCB2 /* SSHolidayUS.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = SSHolidayUS.m; path = SSLunarDate/SSHolidayUS.m; sourceTree = ""; }; 5029DA551AA8111F00712B83 /* UIScrollView+FSExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIScrollView+FSExtension.h"; path = "Pod/Classes/UIScrollView+FSExtension.h"; sourceTree = ""; }; 5029DA561AA8111F00712B83 /* UIScrollView+FSExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIScrollView+FSExtension.m"; path = "Pod/Classes/UIScrollView+FSExtension.m"; sourceTree = ""; }; + 5056E8801AB04B1D00550681 /* CALayer+FSExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "CALayer+FSExtension.h"; path = "Pod/Classes/CALayer+FSExtension.h"; sourceTree = ""; }; + 5056E8811AB04B1D00550681 /* CALayer+FSExtension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "CALayer+FSExtension.m"; path = "Pod/Classes/CALayer+FSExtension.m"; sourceTree = ""; }; 507253B2398C97027677F2D4 /* Pods-FSCalendar-SSLunarDate-prefix.pch */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.h; path = "Pods-FSCalendar-SSLunarDate-prefix.pch"; sourceTree = ""; }; 54F8D017A120281736F206D6 /* UIView+FSExtension.h */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.h; name = "UIView+FSExtension.h"; path = "Pod/Classes/UIView+FSExtension.h"; sourceTree = ""; }; 592634D55D346607A4B7530F /* FSCalendarPage.m */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.c.objc; name = FSCalendarPage.m; path = Pod/Classes/FSCalendarPage.m; sourceTree = ""; }; @@ -275,6 +278,8 @@ 2AF241AFE2CAC20B7AE30AD0 /* NSDate+FSExtension.m */, 54F8D017A120281736F206D6 /* UIView+FSExtension.h */, 8ABD412B8358CA722FC65E56 /* UIView+FSExtension.m */, + 5056E8801AB04B1D00550681 /* CALayer+FSExtension.h */, + 5056E8811AB04B1D00550681 /* CALayer+FSExtension.m */, 5029DA551AA8111F00712B83 /* UIScrollView+FSExtension.h */, 5029DA561AA8111F00712B83 /* UIScrollView+FSExtension.m */, D2F28E91854D59AA3E5E6865 /* Support Files */, @@ -616,6 +621,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 5056E8821AB04B1D00550681 /* CALayer+FSExtension.m in Sources */, 969FDB3DE912C57F822E2420 /* Pods-FSCalendar-dummy.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Pod/Classes/CALayer+FSExtension.h b/Pod/Classes/CALayer+FSExtension.h new file mode 100644 index 0000000..3509c8d --- /dev/null +++ b/Pod/Classes/CALayer+FSExtension.h @@ -0,0 +1,21 @@ +// +// CALayer+FSExtension.h +// Pods +// +// Created by Wenchao Ding on 11/3/15. +// +// + +#import + +@interface CALayer (FSExtension) + +@property (nonatomic) CGFloat fs_width; +@property (nonatomic) CGFloat fs_height; + +@property (nonatomic) CGFloat fs_top; +@property (nonatomic) CGFloat fs_left; +@property (nonatomic) CGFloat fs_bottom; +@property (nonatomic) CGFloat fs_right; + +@end diff --git a/Pod/Classes/CALayer+FSExtension.m b/Pod/Classes/CALayer+FSExtension.m new file mode 100644 index 0000000..e44b28e --- /dev/null +++ b/Pod/Classes/CALayer+FSExtension.m @@ -0,0 +1,73 @@ +// +// CALayer+FSExtension.m +// Pods +// +// Created by Wenchao Ding on 11/3/15. +// +// + +#import "CALayer+FSExtension.h" + +@implementation CALayer (FSExtension) + +- (CGFloat)fs_width +{ + return CGRectGetWidth(self.frame); +} + +- (void)setFs_width:(CGFloat)fs_width +{ + self.frame = CGRectMake(self.fs_left, self.fs_top, fs_width, self.fs_height); +} + +- (CGFloat)fs_height +{ + return CGRectGetHeight(self.frame); +} + +- (void)setFs_height:(CGFloat)fs_height +{ + self.frame = CGRectMake(self.fs_left, self.fs_top, self.fs_width, fs_height); +} + +- (CGFloat)fs_top +{ + return CGRectGetMinY(self.frame); +} + +- (void)setFs_top:(CGFloat)fs_top +{ + self.frame = CGRectMake(self.fs_left, fs_top, self.fs_width, self.fs_height); +} + +- (CGFloat)fs_bottom +{ + return CGRectGetMaxY(self.frame); +} + +- (void)setFs_bottom:(CGFloat)fs_bottom +{ + self.fs_top = fs_bottom - self.fs_height; +} + +- (CGFloat)fs_left +{ + return CGRectGetMinX(self.frame); +} + +- (void)setFs_left:(CGFloat)fs_left +{ + self.frame = CGRectMake(fs_left, self.fs_top, self.fs_width, self.fs_height); +} + +- (CGFloat)fs_right +{ + return CGRectGetMaxX(self.frame); +} + +- (void)setFs_right:(CGFloat)fs_right +{ + self.fs_left = self.fs_right - self.fs_width; +} + +@end diff --git a/Pod/Classes/FSCalendar.m b/Pod/Classes/FSCalendar.m index 729cc33..4d0c182 100644 --- a/Pod/Classes/FSCalendar.m +++ b/Pod/Classes/FSCalendar.m @@ -16,6 +16,8 @@ #define kWeekHeight roundf(self.fs_height/9) #define kBlueText [UIColor colorWithRed:14/255.0 green:69/255.0 blue:221/255.0 alpha:1.0] +#define kPink [UIColor colorWithRed:198/255.0 green:51/255.0 blue:42/255.0 alpha:1.0] +#define kBlue [UIColor colorWithRed:31/255.0 green:119/255.0 blue:219/255.0 alpha:1.0] const char * flowKey; @@ -26,6 +28,10 @@ const char * flowKey; @property (strong, nonatomic) FSCalendarPage *page0; @property (strong, nonatomic) FSCalendarPage *page1; +@property (strong, nonatomic) NSMutableDictionary *unitColors; +@property (strong, nonatomic) NSMutableDictionary *titleColors; +@property (strong, nonatomic) NSMutableDictionary *subtitleColors; + @property (assign, nonatomic) NSInteger currentPage; @property (assign, nonatomic) CGFloat baseOffset; @@ -40,6 +46,7 @@ const char * flowKey; - (NSString *)subtitleForDate:(NSDate *)date; - (void)adjustTitleIfNecessary; +- (void)reloadUnits; @end @@ -113,8 +120,31 @@ const char * flowKey; [_page1.subviews setValue:self forKeyPath:@"dataSource"]; [_page1.subviews setValue:self forKeyPath:@"delegate"]; + + _unitColors = [NSMutableDictionary dictionaryWithCapacity:4]; + _unitColors[@(FSCalendarUnitStateNormal)] = [UIColor clearColor]; + _unitColors[@(FSCalendarUnitStateSelected)] = kBlue; + _unitColors[@(FSCalendarUnitStateDisabled)] = [UIColor clearColor]; + _unitColors[@(FSCalendarUnitStatePlaceholder)] = [UIColor clearColor]; + _unitColors[@(FSCalendarUnitStateToday)] = kPink; + + _titleColors = [NSMutableDictionary dictionaryWithCapacity:4]; + _titleColors[@(FSCalendarUnitStateNormal)] = [UIColor darkTextColor]; + _titleColors[@(FSCalendarUnitStateWeekend)] = [UIColor darkTextColor]; + _titleColors[@(FSCalendarUnitStateSelected)] = [UIColor whiteColor]; + _titleColors[@(FSCalendarUnitStateDisabled)] = [UIColor grayColor]; + _titleColors[@(FSCalendarUnitStatePlaceholder)] = [UIColor lightGrayColor]; + _titleColors[@(FSCalendarUnitStateToday)] = [UIColor whiteColor]; + + _subtitleColors = [NSMutableDictionary dictionaryWithCapacity:4]; + _subtitleColors[@(FSCalendarUnitStateNormal)] = [UIColor darkGrayColor]; + _subtitleColors[@(FSCalendarUnitStateWeekend)] = [UIColor darkGrayColor]; + _subtitleColors[@(FSCalendarUnitStateSelected)] = [UIColor whiteColor]; + _subtitleColors[@(FSCalendarUnitStateDisabled)] = [UIColor lightGrayColor]; + _subtitleColors[@(FSCalendarUnitStatePlaceholder)] = [UIColor lightGrayColor]; + _subtitleColors[@(FSCalendarUnitStateToday)] = [UIColor whiteColor]; + _unitStyle = FSCalendarUnitStyleCircle; - _autoAdjustTitleSize = YES; } @@ -265,122 +295,122 @@ const char * flowKey; - (void)setTitleDefaultColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateNormal]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateNormal]; - }]; + if (color) { + _titleColors[@(FSCalendarUnitStateNormal)] = color; + } else { + [_titleColors removeObjectForKey:@(FSCalendarUnitStateNormal)]; + } + [self reloadUnits]; } - (void)setTitleSelectionColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateSelected]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateSelected]; - }]; + if (color) { + _titleColors[@(FSCalendarUnitStateSelected)] = color; + } else { + [_titleColors removeObjectForKey:@(FSCalendarUnitStateSelected)]; + } + [self reloadUnits]; } - (void)setTitleTodayColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateToday]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateToday]; - }]; + if (color) { + _titleColors[@(FSCalendarUnitStateToday)] = color; + } else { + [_titleColors removeObjectForKey:@(FSCalendarUnitStateToday)]; + } + [self reloadUnits]; } - (void)setTitlePlaceholderColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStatePlaceholder]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStatePlaceholder]; - }]; + if (color) { + _titleColors[@(FSCalendarUnitStatePlaceholder)] = color; + } else { + [_titleColors removeObjectForKey:@(FSCalendarUnitStatePlaceholder)]; + } + [self reloadUnits]; } - (void)setTitleWeekendColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateWeekend]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setTitleColor:color forState:FSCalendarUnitStateWeekend]; - }]; + if (color) { + _titleColors[@(FSCalendarUnitStateWeekend)] = color; + } else { + [_titleColors removeObjectForKey:@(FSCalendarUnitStateWeekend)]; + } + [self reloadUnits]; } - (void)setSubtitleDefaultColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateNormal]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateNormal]; - }]; + if (color) { + _subtitleColors[@(FSCalendarUnitStateNormal)] = color; + } else { + [_subtitleColors removeObjectForKey:@(FSCalendarUnitStateNormal)]; + } + [self reloadUnits]; } - (void)setSubtitleSelectionColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateSelected]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateSelected]; - }]; + if (color) { + _subtitleColors[@(FSCalendarUnitStateSelected)] = color; + } else { + [_subtitleColors removeObjectForKey:@(FSCalendarUnitStateSelected)]; + } + [self reloadUnits]; } - (void)setSubtitleTodayColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateToday]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateToday]; - }]; + if (color) { + _subtitleColors[@(FSCalendarUnitStateToday)] = color; + } else { + [_subtitleColors removeObjectForKey:@(FSCalendarUnitStateToday)]; + } + [self reloadUnits]; } - (void)setSubtitlePlaceholderColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStatePlaceholder]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStatePlaceholder]; - }]; + if (color) { + _subtitleColors[@(FSCalendarUnitStatePlaceholder)] = color; + } else { + [_subtitleColors removeObjectForKey:@(FSCalendarUnitStatePlaceholder)]; + } + [self reloadUnits]; } - (void)setSubtitleWeekendColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateWeekend]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setSubtitleColor:color forState:FSCalendarUnitStateWeekend]; - }]; + if (color) { + _subtitleColors[@(FSCalendarUnitStateWeekend)] = color; + } else { + [_subtitleColors removeObjectForKey:@(FSCalendarUnitStateWeekend)]; + } + [self reloadUnits]; } - (void)setSelectionColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setUnitColor:color forState:FSCalendarUnitStateSelected]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setUnitColor:color forState:FSCalendarUnitStateSelected]; - }]; + if (color) { + _unitColors[@(FSCalendarUnitStateSelected)] = color; + } else { + [_unitColors removeObjectForKey:@(FSCalendarUnitStateSelected)]; + } + [self reloadUnits]; } - (void)setTodayColor:(UIColor *)color { - [_page0.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setUnitColor:color forState:FSCalendarUnitStateToday]; - }]; - [_page1.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - [(FSCalendarUnit *)obj setUnitColor:color forState:FSCalendarUnitStateToday]; - }]; + if (color) { + _unitColors[@(FSCalendarUnitStateToday)] = color; + } else { + [_unitColors removeObjectForKey:@(FSCalendarUnitStateToday)]; + } + [self reloadUnits]; } - (void)setEventColor:(UIColor *)color @@ -457,6 +487,12 @@ const char * flowKey; } } +- (void)reloadUnits +{ + [_page0.subviews makeObjectsPerformSelector:@selector(setNeedsLayout)]; + [_page1.subviews makeObjectsPerformSelector:@selector(setNeedsLayout)]; +} + - (void)updatePointer { switch (self.flow) { @@ -628,6 +664,21 @@ const char * flowKey; return selected; } +- (UIColor *)unitColorForUnit:(FSCalendarUnit *)unit +{ + return _unitColors[@(unit.absoluteState)]; +} + +- (UIColor *)titleColorForUnit:(FSCalendarUnit *)unit +{ + return _titleColors[@(unit.absoluteState)]; +} + +- (UIColor *)subtitleColorForUnit:(FSCalendarUnit *)unit +{ + return _subtitleColors[@(unit.absoluteState)]; +} + #pragma mark - Unit Delegate - (void)handleUnitTap:(FSCalendarUnit *)unit diff --git a/Pod/Classes/FSCalendarUnit.h b/Pod/Classes/FSCalendarUnit.h index 0b41b7c..7e50237 100644 --- a/Pod/Classes/FSCalendarUnit.h +++ b/Pod/Classes/FSCalendarUnit.h @@ -30,17 +30,10 @@ @property (nonatomic, readonly, getter = isToday) BOOL today; @property (nonatomic, readonly, getter = isWeekend) BOOL weekend; +@property (nonatomic, readonly) FSCalendarUnitState absoluteState; + @property (strong, nonatomic) NSDate *date; -- (void)setUnitColor:(UIColor *)unitColor forState:(FSCalendarUnitState)state; -- (UIColor *)unitColorForState:(FSCalendarUnitState)state; - -- (void)setTitleColor:(UIColor *)titleColor forState:(FSCalendarUnitState)state; -- (UIColor *)titleColorForState:(FSCalendarUnitState)state; - -- (void)setSubtitleColor:(UIColor *)subtitleColor forState:(FSCalendarUnitState)state; -- (UIColor *)subtitleColorForState:(FSCalendarUnitState)state; - @end @protocol FSCalendarUnitDataSource @@ -52,6 +45,10 @@ - (NSString *)subtitleForUnit:(FSCalendarUnit *)unit; - (BOOL)hasEventForUnit:(FSCalendarUnit *)unit; +- (UIColor *)unitColorForUnit:(FSCalendarUnit *)unit; +- (UIColor *)titleColorForUnit:(FSCalendarUnit *)unit; +- (UIColor *)subtitleColorForUnit:(FSCalendarUnit *)unit; + @end @protocol FSCalendarUnitDelegate diff --git a/Pod/Classes/FSCalendarUnit.m b/Pod/Classes/FSCalendarUnit.m index f5a62cc..e8a334b 100644 --- a/Pod/Classes/FSCalendarUnit.m +++ b/Pod/Classes/FSCalendarUnit.m @@ -12,8 +12,6 @@ #import "UIView+FSExtension.h" #define kAnimationDuration 0.12 -#define kPink [UIColor colorWithRed:198/255.0 green:51/255.0 blue:42/255.0 alpha:1.0] -#define kBlue [UIColor colorWithRed:31/255.0 green:119/255.0 blue:219/255.0 alpha:1.0] #define kTitleHeight self.fs_height*5.0/6.0 #define kDiameter MIN(self.fs_height*5.0/6.0,self.fs_width) @@ -21,12 +19,7 @@ @interface FSCalendarUnit () @property (strong, nonatomic) UILabel *titleLabel; - -@property (readonly, nonatomic) FSCalendarUnitState absoluteState; - -@property (strong, nonatomic) NSMutableDictionary *unitColors; -@property (strong, nonatomic) NSMutableDictionary *titleColors; -@property (strong, nonatomic) NSMutableDictionary *subtitleColors; +@property (strong, nonatomic) UILabel *subtitleLabel; @property (strong, nonatomic) CAShapeLayer *animLayer; @property (strong, nonatomic) CAShapeLayer *eventLayer; @@ -44,37 +37,21 @@ self = [super initWithFrame:frame]; if (self) { + self.layer.borderWidth = 1.0; _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.numberOfLines = 1; _titleLabel.font = [UIFont systemFontOfSize:16]; [self addSubview:_titleLabel]; + _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; + _subtitleLabel.textAlignment = NSTextAlignmentCenter; + _subtitleLabel.numberOfLines = 1; + _subtitleLabel.font = [UIFont systemFontOfSize:11]; + [self addSubview:_subtitleLabel]; + _titleFont = _titleLabel.font; - _subtitleFont = [_titleFont fontWithSize:11]; - - _unitColors = [NSMutableDictionary dictionaryWithCapacity:4]; - _unitColors[@(FSCalendarUnitStateNormal)] = [UIColor clearColor]; - _unitColors[@(FSCalendarUnitStateSelected)] = kBlue; - _unitColors[@(FSCalendarUnitStateDisabled)] = [UIColor clearColor]; - _unitColors[@(FSCalendarUnitStatePlaceholder)] = [UIColor clearColor]; - _unitColors[@(FSCalendarUnitStateToday)] = kPink; - - _titleColors = [NSMutableDictionary dictionaryWithCapacity:4]; - _titleColors[@(FSCalendarUnitStateNormal)] = [UIColor darkTextColor]; - _titleColors[@(FSCalendarUnitStateWeekend)] = [UIColor darkTextColor]; - _titleColors[@(FSCalendarUnitStateSelected)] = [UIColor whiteColor]; - _titleColors[@(FSCalendarUnitStateDisabled)] = [UIColor grayColor]; - _titleColors[@(FSCalendarUnitStatePlaceholder)] = [UIColor lightGrayColor]; - _titleColors[@(FSCalendarUnitStateToday)] = [UIColor whiteColor]; - - _subtitleColors = [NSMutableDictionary dictionaryWithCapacity:4]; - _subtitleColors[@(FSCalendarUnitStateNormal)] = [UIColor darkGrayColor]; - _subtitleColors[@(FSCalendarUnitStateWeekend)] = [UIColor darkGrayColor]; - _subtitleColors[@(FSCalendarUnitStateSelected)] = [UIColor whiteColor]; - _subtitleColors[@(FSCalendarUnitStateDisabled)] = [UIColor lightGrayColor]; - _subtitleColors[@(FSCalendarUnitStatePlaceholder)] = [UIColor lightGrayColor]; - _subtitleColors[@(FSCalendarUnitStateToday)] = [UIColor whiteColor]; + _subtitleFont = _subtitleLabel.font; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; tapGesture.numberOfTapsRequired = 1; @@ -119,7 +96,7 @@ default: break; } - _animLayer.fillColor = [self unitColorForState:self.absoluteState].CGColor; + _animLayer.fillColor = [self.dataSource unitColorForUnit:self].CGColor; CGFloat eventSize = _animLayer.frame.size.height/6.0; _eventLayer.frame = CGRectMake((_animLayer.frame.size.width-eventSize)/2+_animLayer.frame.origin.x, CGRectGetMaxY(_animLayer.frame)+eventSize*0.2, eventSize*0.8, eventSize*0.8); @@ -139,28 +116,23 @@ if (_date) { // set attribute title - _titleLabel.numberOfLines = 1; - - NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", @(_date.fs_day)] attributes: - @{NSForegroundColorAttributeName:[self titleColorForState:self.absoluteState],NSFontAttributeName:_titleFont}]; NSString *subtitle = [_dataSource subtitleForUnit:self]; - if (subtitle && subtitle.length) { - // set subtitle - _titleLabel.numberOfLines = 2; - NSAttributedString *subtitleString = [[NSAttributedString alloc] initWithString:subtitle attributes:@{NSForegroundColorAttributeName:[self subtitleColorForState:self.absoluteState], NSFontAttributeName: _subtitleFont}]; - [attributeString appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]]; - [attributeString appendAttributedString:subtitleString]; - // adjust line height - NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; - paragraph.lineSpacing = 0; - paragraph.alignment = NSTextAlignmentCenter; - paragraph.maximumLineHeight = _titleFont.lineHeight * 0.6; - [attributeString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, attributeString.length)]; - _titleLabel.fs_top += _titleFont.lineHeight * 0.1; + UIColor *titleColor = [self.dataSource titleColorForUnit:self]; + _titleLabel.textColor = titleColor; + _titleLabel.text = [NSString stringWithFormat:@"%@", @(_date.fs_day)]; + if (subtitle) { + _subtitleLabel.hidden = NO; + _subtitleLabel.text = subtitle; + [_titleLabel sizeToFit]; + [_subtitleLabel sizeToFit]; + CGFloat totalTextSize = _titleLabel.fs_height + _subtitleLabel.fs_height; + _titleLabel.frame = CGRectMake(0, (self.fs_height-totalTextSize) * 0.5, self.fs_width, _titleLabel.fs_height); + _subtitleLabel.fs_bottom = _titleLabel.fs_top; + _subtitleLabel.textColor = [self.dataSource subtitleColorForUnit:self]; } else { _titleLabel.fs_top -= 1; + _subtitleLabel.hidden = YES; } - _titleLabel.attributedText = attributeString; } } @@ -174,60 +146,6 @@ [self setNeedsLayout]; } -- (void)setUnitColor:(UIColor *)backgroundColor forState:(FSCalendarUnitState)state -{ - if ([[self unitColorForState:state] isEqual:backgroundColor]) { - return; - } - if (backgroundColor) { - _unitColors[@(state)] = backgroundColor; - } else { - [_unitColors removeObjectForKey:@(state)]; - } - [self setNeedsLayout]; -} - -- (UIColor *)unitColorForState:(FSCalendarUnitState)state -{ - return _unitColors[@(state)]; -} - -- (void)setTitleColor:(UIColor *)titleColor forState:(FSCalendarUnitState)state -{ - if ([[self titleColorForState:state] isEqual:titleColor]) { - return; - } - if (titleColor) { - _titleColors[@(state)] = titleColor; - } else { - [_titleColors removeObjectForKey:@(state)]; - } - [self setNeedsLayout]; -} - -- (UIColor *)titleColorForState:(FSCalendarUnitState)state -{ - return _titleColors[@(state)]; -} - -- (void)setSubtitleColor:(UIColor *)subtitleColor forState:(FSCalendarUnitState)state -{ - if ([[self subtitleColorForState:state] isEqual:subtitleColor]) { - return; - } - if (subtitleColor) { - _subtitleColors[@(state)] = subtitleColor; - } else { - [_subtitleColors removeObjectForKey:@(state)]; - } - [self setNeedsLayout]; -} - -- (UIColor *)subtitleColorForState:(FSCalendarUnitState)state -{ - return _subtitleColors[@(state)]; -} - - (void)setTitleFont:(UIFont *)titleFont { if (_titleFont != titleFont) {