1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// RollViewController.m
// FSCalendar
//
// Created by dingwenchao on 10/16/15.
// Copyright (c) 2015 Wenchao Ding. All rights reserved.
//
#import "DelegateAppearanceViewController.h"
#define kViolet [UIColor colorWithRed:170/255.0 green:114/255.0 blue:219/255.0 alpha:1.0]
NS_ASSUME_NONNULL_BEGIN
@interface DelegateAppearanceViewController()<FSCalendarDataSource,FSCalendarDelegate,FSCalendarDelegateAppearance>
@property (weak, nonatomic) FSCalendar *calendar;
@property (strong, nonatomic) NSCalendar *gregorian;
@property (strong, nonatomic) NSDateFormatter *dateFormatter1;
@property (strong, nonatomic) NSDateFormatter *dateFormatter2;
@property (strong, nonatomic) NSDictionary *fillSelectionColors;
@property (strong, nonatomic) NSDictionary *fillDefaultColors;
@property (strong, nonatomic) NSDictionary *borderDefaultColors;
@property (strong, nonatomic) NSDictionary *borderSelectionColors;
@property (strong, nonatomic) NSArray *datesWithEvent;
@property (strong, nonatomic) NSArray *datesWithMultipleEvents;
@end
NS_ASSUME_NONNULL_END
@implementation DelegateAppearanceViewController
#pragma mark - Life cycle
- (instancetype)init
{
self = [super init];
if (self) {
self.title = @"FSCalendar";
self.fillDefaultColors = @{@"2020/10/08":[UIColor purpleColor],
@"2020/10/06":[UIColor greenColor],
@"2020/10/18":[UIColor cyanColor],
@"2020/10/22":[UIColor yellowColor],
@"2020/11/08":[UIColor purpleColor],
@"2020/11/06":[UIColor greenColor],
@"2020/11/18":[UIColor cyanColor],
@"2020/11/22":[UIColor yellowColor],
@"2020/12/08":[UIColor purpleColor],
@"2020/12/06":[UIColor greenColor],
@"2020/12/18":[UIColor cyanColor],
@"2020/12/22":[UIColor magentaColor]};
self.fillSelectionColors = @{@"2020/10/08":[UIColor greenColor],
@"2020/10/06":[UIColor purpleColor],
@"2020/10/17":[UIColor grayColor],
@"2020/10/21":[UIColor cyanColor],
@"2020/11/08":[UIColor greenColor],
@"2020/11/06":[UIColor purpleColor],
@"2020/11/17":[UIColor grayColor],
@"2020/11/21":[UIColor cyanColor],
@"2020/12/08":[UIColor greenColor],
@"2020/12/06":[UIColor purpleColor],
@"2020/12/17":[UIColor grayColor],
@"2020/12/21":[UIColor cyanColor]};
self.borderDefaultColors = @{@"2020/10/08":[UIColor brownColor],
@"2020/10/17":[UIColor magentaColor],
@"2020/10/21":FSCalendarStandardSelectionColor,
@"2020/10/25":[UIColor blackColor],
@"2020/11/08":[UIColor brownColor],
@"2020/11/17":[UIColor magentaColor],
@"2020/11/21":FSCalendarStandardSelectionColor,
@"2020/11/25":[UIColor blackColor],
@"2020/12/08":[UIColor brownColor],
@"2020/12/17":[UIColor magentaColor],
@"2020/12/21":FSCalendarStandardSelectionColor,
@"2020/12/25":[UIColor blackColor]};
self.borderSelectionColors = @{@"2020/10/08":[UIColor redColor],
@"2020/10/17":[UIColor purpleColor],
@"2020/10/21":FSCalendarStandardSelectionColor,
@"2020/10/25":FSCalendarStandardTodayColor,
@"2020/11/08":[UIColor redColor],
@"2020/11/17":[UIColor purpleColor],
@"2020/11/21":FSCalendarStandardSelectionColor,
@"2020/11/25":FSCalendarStandardTodayColor,
@"2020/12/08":[UIColor redColor],
@"2020/12/17":[UIColor purpleColor],
@"2020/12/21":FSCalendarStandardSelectionColor,
@"2020/12/25":FSCalendarStandardTodayColor};
self.datesWithEvent = @[@"2020-10-03",
@"2020-10-06",
@"2020-10-12",
@"2020-10-25"];
self.datesWithMultipleEvents = @[@"2020-10-08",
@"2020-10-16",
@"2020-10-20",
@"2020-10-28"];
self.gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
self.dateFormatter1 = [[NSDateFormatter alloc] init];
self.dateFormatter1.dateFormat = @"yyyy/MM/dd";
self.dateFormatter2 = [[NSDateFormatter alloc] init];
self.dateFormatter2.dateFormat = @"yyyy-MM-dd";
}
return self;
}
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.view = view;
CGFloat height = [[UIDevice currentDevice].model hasPrefix:@"iPad"] ? 450 : 300;
FSCalendar *calendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, height)];
calendar.dataSource = self;
calendar.delegate = self;
calendar.allowsMultipleSelection = YES;
calendar.swipeToChooseGesture.enabled = YES;
calendar.backgroundColor = [UIColor whiteColor];
calendar.appearance.caseOptions = FSCalendarCaseOptionsHeaderUsesUpperCase|FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
[self.view addSubview:calendar];
self.calendar = calendar;
[calendar setCurrentPage:[self.dateFormatter1 dateFromString:@"2020/10/03"] animated:NO];
UIBarButtonItem *todayItem = [[UIBarButtonItem alloc] initWithTitle:@"TODAY" style:UIBarButtonItemStylePlain target:self action:@selector(todayItemClicked:)];
self.navigationItem.rightBarButtonItem = todayItem;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// For UITest
self.calendar.accessibilityIdentifier = @"calendar";
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
}
#pragma mark - Target actions
- (void)todayItemClicked:(id)sender
{
[_calendar setCurrentPage:[NSDate date] animated:NO];
}
#pragma mark - <FSCalendarDataSource>
- (NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date
{
NSString *dateString = [self.dateFormatter2 stringFromDate:date];
if ([_datesWithEvent containsObject:dateString]) {
return 1;
}
if ([_datesWithMultipleEvents containsObject:dateString]) {
return 3;
}
return 0;
}
#pragma mark - <FSCalendarDelegateAppearance>
- (NSArray *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventDefaultColorsForDate:(NSDate *)date
{
NSString *dateString = [self.dateFormatter2 stringFromDate:date];
if ([_datesWithMultipleEvents containsObject:dateString]) {
return @[[UIColor magentaColor],appearance.eventDefaultColor,[UIColor blackColor]];
}
return nil;
}
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance fillSelectionColorForDate:(NSDate *)date
{
NSString *key = [self.dateFormatter1 stringFromDate:date];
if ([_fillSelectionColors.allKeys containsObject:key]) {
return _fillSelectionColors[key];
}
return appearance.selectionColor;
}
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance fillDefaultColorForDate:(NSDate *)date
{
NSString *key = [self.dateFormatter1 stringFromDate:date];
if ([_fillDefaultColors.allKeys containsObject:key]) {
return _fillDefaultColors[key];
}
return nil;
}
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderDefaultColorForDate:(NSDate *)date
{
NSString *key = [self.dateFormatter1 stringFromDate:date];
if ([_borderDefaultColors.allKeys containsObject:key]) {
return _borderDefaultColors[key];
}
return appearance.borderDefaultColor;
}
- (UIColor *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderSelectionColorForDate:(NSDate *)date
{
NSString *key = [self.dateFormatter1 stringFromDate:date];
if ([_borderSelectionColors.allKeys containsObject:key]) {
return _borderSelectionColors[key];
}
return appearance.borderSelectionColor;
}
- (CGFloat)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance borderRadiusForDate:(nonnull NSDate *)date
{
if ([@[@8,@17,@21,@25] containsObject:@([self.gregorian component:NSCalendarUnitDay fromDate:date])]) {
return 0.0;
}
return 1.0;
}
@end