-
Notifications
You must be signed in to change notification settings - Fork 19
/
ASDayPicker.m
391 lines (308 loc) · 13.2 KB
/
ASDayPicker.m
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//
// ASDayPicker
// http://github.com/appscape/ASDayPicker
//
// Copyright (c) 2014 appscape. All rights reserved.
//
#import "ASDayPicker.h"
static const UIEdgeInsets kDefaultInsets = {0,6.0f,0,6.0f};
static const CGFloat kWeekdayLabelHeight = 20.0f;
@interface ASDayPickerScrollView : UIScrollView
@end
@implementation ASDayPickerScrollView
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
@end
@interface ASDayPicker()<UIScrollViewDelegate> {
NSCalendar *_calendar;
NSArray *_weekdayTitles;
NSMutableArray *_weekdayLabels;
UIScrollView *_daysScrollView;
CGFloat _dx, _dh;
CGFloat _scrollStartOffsetX;
NSMutableArray *_days;
UIButton *_lastSelectedButton;
}
@end
@implementation ASDayPicker
- (void)setup {
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
_edgeInsets = kDefaultInsets;
_daysScrollView = [[ASDayPickerScrollView alloc] initWithFrame:CGRectZero];
_daysScrollView.showsHorizontalScrollIndicator = NO;
_daysScrollView.pagingEnabled = YES;
_daysScrollView.delegate = self;
_daysScrollView.canCancelContentTouches = YES;
_daysScrollView.delaysContentTouches = YES;
_daysScrollView.decelerationRate = UIScrollViewDecelerationRateFast;
[self addSubview:_daysScrollView];
_weekdayLabels = [NSMutableArray array];
for (NSUInteger i = 0;i<7;i++) {
UILabel *l = [[UILabel alloc] initWithFrame:CGRectZero];
l.textAlignment = NSTextAlignmentCenter;
[_weekdayLabels addObject:l];
[self addSubview:l];
}
self.weekdayTitles = [ASDayPicker weekdayTitlesWithLocaleIdentifier:nil
length:1
uppercase:YES];
self.selectedDateBackgroundImage = [ASDayPicker imageWithColor:self.tintColor];
self.selectedDateTextColor = [UIColor whiteColor];
self.dateTextColor = [UIColor blackColor];
self.outOfRangeDateTextColor = [UIColor colorWithWhite:0.8f alpha:1.0f];
self.dateFont = [UIFont systemFontOfSize:17.0f];
self.weekdayFont = [UIFont systemFontOfSize:12.0f];
self.weekdayTextColor = [UIColor blackColor];
[self setSelectedDate:[self dateWithoutTimeFromDate:[NSDate date]] recenter:NO];
}
- (void)setSelectedDate:(NSDate *)selectedDate {
[self setSelectedDate:selectedDate recenter:YES];
}
- (void)setSelectedDate:(NSDate *)selectedDate recenter:(BOOL)recenter {
[self willChangeValueForKey:@"selectedDate"];
[self willChangeValueForKey:@"selectedWeekday"];
_selectedDate = selectedDate;
NSDateComponents *components = [_calendar components:NSWeekdayCalendarUnit fromDate:_selectedDate];
NSInteger d = components.weekday - 2;
if (d < 0) d = 7 + d;
_selectedWeekday = d;
[self recolorWeekdays];
[self didChangeValueForKey:@"selectedWeekday"];
[self didChangeValueForKey:@"selectedDate"];
if (recenter) [self recenter];
}
- (void)setSelectedDateBackgroundColor:(UIColor *)selectedDateBackgroundColor {
_selectedDateBackgroundColor = selectedDateBackgroundColor;
self.selectedDateBackgroundImage = [ASDayPicker imageWithColor:selectedDateBackgroundColor];
}
- (void)setSelectedDateBackgroundImage:(UIImage *)selectedDateBackgroundImage {
_selectedDateBackgroundImage = selectedDateBackgroundImage;
[self setNeedsLayout];
}
- (void)setEdgeInsets:(UIEdgeInsets)edgeInsets {
_edgeInsets = edgeInsets;
[self setNeedsLayout];
}
- (void)setWeekdayTitles:(NSArray *)weekdayTitles {
NSParameterAssert(weekdayTitles.count == 7);
_weekdayTitles = weekdayTitles;
for (int i=0;i<7;i++) {
((UILabel*)_weekdayLabels[i]).text = _weekdayTitles[i];
}
}
- (void)recolorWeekdays {
for (int i=0;i<7;i++) {
((UILabel*)_weekdayLabels[i]).textColor = (i == _selectedWeekday) ? _selectedWeekdayTextColor : _weekdayTextColor;
}
}
- (void)setWeekdayTextColor:(UIColor *)weekdayTextColor {
_weekdayTextColor = weekdayTextColor;
[self recolorWeekdays];
}
- (void)setSelectedWeekdayTextColor:(UIColor *)selectedWeekdayTextColor {
_selectedWeekdayTextColor = selectedWeekdayTextColor;
[self recolorWeekdays];
}
- (void)setWeekdayFont:(UIFont *)font {
_weekdayFont = font;
for (int i=0;i<7;i++) {
((UILabel*)_weekdayLabels[i]).font = _weekdayFont;
}
}
- (void)setSelectedDateBackgroundExtendsToTop:(BOOL)selectedDateBackgroundExtendsToTop {
_selectedDateBackgroundExtendsToTop = selectedDateBackgroundExtendsToTop;
[self setNeedsLayout];
}
- (void)awakeFromNib {
[self setup];
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (void)layoutSubviews {
_dh = self.frame.size.height - kWeekdayLabelHeight;
_daysScrollView.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
_dx = (self.frame.size.width - (_edgeInsets.left + _edgeInsets.right)) / 7.0f;
for (NSUInteger i=0;i<7;i++) {
UILabel *l = _weekdayLabels[i];
l.frame = CGRectMake(_edgeInsets.left + _dx * i, 0, _dx, kWeekdayLabelHeight);
}
[self recenter];
}
- (UIButton*)buttonForDate:(NSDate*)date index:(NSUInteger)index {
UIButton *b = [[UIButton alloc] init];
[b setTitleColor:self.outOfRangeDateTextColor forState:UIControlStateDisabled];
[b setTitleColor:self.dateTextColor forState:UIControlStateNormal];
[b setTitleColor:self.dateTextColor forState:UIControlStateHighlighted];
[b setTitleColor:self.selectedDateTextColor forState:UIControlStateSelected];
[b setTitleColor:self.selectedDateTextColor forState:UIControlStateSelected | UIControlStateHighlighted];
[b setBackgroundImage:self.selectedDateBackgroundImage forState:UIControlStateSelected];
[b setBackgroundImage:self.selectedDateBackgroundImage forState:UIControlStateSelected | UIControlStateHighlighted];
[b setTitleColor:self.dateTextColor forState:UIControlStateHighlighted];
[b.titleLabel setFont:self.dateFont];
// [b.titleLabel setTextAlignment:NSTextAlignmentCenter];
if (self.selectedDateBackgroundExtendsToTop) {
b.contentEdgeInsets = UIEdgeInsetsMake(kWeekdayLabelHeight/2.0, 0, 0, 0);
}
b.selected = [date isEqualToDate:_selectedDate];
if ((_startDate && [date compare:_startDate] == NSOrderedAscending) ||
(_endDate && [date compare:_endDate] == NSOrderedDescending)) {
b.enabled = NO;
}
b.tag = index;
b.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// b.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
NSDateComponents *components = [_calendar components:NSDayCalendarUnit fromDate:date];
[b setTitle:[NSString stringWithFormat:@"%ld", (long)components.day] forState:UIControlStateNormal];
[b addTarget:self action:@selector(dayButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
return b;
}
- (void)dayButtonPressed:(UIButton*)sender {
_lastSelectedButton.selected = NO;
sender.selected = YES;
_lastSelectedButton = sender;
[self setSelectedDate:_days[sender.tag] recenter:NO];
}
- (void)recenter {
for (UIView *v in _daysScrollView.subviews) {
[v removeFromSuperview];
}
_days = [NSMutableArray array];
NSInteger fromIndex = -1;
NSInteger toIndex = 1;
if (_startDate) {
NSDateComponents *weeks = [[NSDateComponents alloc] init];
weeks.day = -7;
NSDate *prevDate = [_calendar dateByAddingComponents:weeks toDate:_selectedDate options:0];
if ([prevDate compare:_startDate] == NSOrderedAscending) {
fromIndex = 0;
}
}
if (_endDate) {
NSDateComponents *weeks = [[NSDateComponents alloc] init];
weeks.day = 7;
NSDate *nextDate = [_calendar dateByAddingComponents:weeks toDate:_selectedDate options:0];
if ([nextDate compare:_endDate] == NSOrderedDescending) {
toIndex = 0;
}
}
CGFloat btnY = self.selectedDateBackgroundExtendsToTop ? 0 : kWeekdayLabelHeight;
CGFloat btnHeight = self.selectedDateBackgroundExtendsToTop ? kWeekdayLabelHeight + _dh : _dh;
for (NSInteger i=fromIndex;i<=toIndex;i++) {
NSArray *days = [self daysForWeekAtIndex:i];
for (NSUInteger j=0;j<days.count;j++) {
UIButton *b = [self buttonForDate:days[j] index:_days.count];
[_days addObject:days[j]];
b.frame = CGRectMake(_edgeInsets.left+(i-fromIndex)*self.frame.size.width+j*_dx, btnY,_dx, btnHeight);
if (b.selected) {
_lastSelectedButton = b;
}
[_daysScrollView addSubview:b];
}
}
NSInteger pages = (toIndex - fromIndex) + 1;
_daysScrollView.contentSize = CGSizeMake(pages * self.frame.size.width, self.frame.size.height);
_daysScrollView.contentOffset = CGPointMake(fromIndex != 0 ? self.frame.size.width : 0, 0);
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
scrollView.userInteractionEnabled = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSInteger dir = 0;
CGFloat dx = scrollView.contentOffset.x - _scrollStartOffsetX;
if (dx > 0) {
dir = 1;
} else if (dx < 0 ){
dir = -1;
}
if (dir != 0) {
NSDateComponents *weeks = [[NSDateComponents alloc] init];
weeks.day = dir * 7;
NSDate *date = [_calendar dateByAddingComponents:weeks toDate:_selectedDate options:0];
// Clip to range
if ((_startDate && [date compare:_startDate] == NSOrderedAscending)) {
date = _startDate;
} else if (_endDate && [date compare:_endDate] == NSOrderedDescending) {
date = _endDate;
}
[self setSelectedDate:date recenter:YES];
} else {
[UIView animateWithDuration:0.25f animations:^{
((UILabel*)_weekdayLabels[_selectedWeekday]).textColor = _selectedWeekdayTextColor;
}];
}
scrollView.userInteractionEnabled = YES;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_scrollStartOffsetX = scrollView.contentOffset.x;
[UIView animateWithDuration:0.25f animations:^{
((UILabel*)_weekdayLabels[_selectedWeekday]).textColor = _weekdayTextColor;
}];
}
- (void)setStartDate:(NSDate *)startDate endDate:(NSDate *)endDate {
NSParameterAssert(!startDate || !endDate || [endDate compare:startDate] != NSOrderedAscending);
_startDate = [self dateWithoutTimeFromDate:startDate];
_endDate = [self dateWithoutTimeFromDate:endDate];
[self setNeedsLayout];
}
// i=0: week containing _selectedDate, i=-1: week before, i=1 week after etc.
- (NSArray*)daysForWeekAtIndex:(NSInteger)i {
NSDateComponents *weeks = [[NSDateComponents alloc] init];
weeks.day = i * 7;
NSDate *d = [_calendar dateByAddingComponents:weeks toDate:_selectedDate options:0];
NSDateComponents *components = [_calendar components:NSWeekdayCalendarUnit fromDate:d];
NSInteger before = components.weekday - 2;
if (before < 0) before = 7 + before;
NSInteger after = 6 - before;
NSMutableArray *result = [NSMutableArray array];
for (NSUInteger i = before;i>0;i--) {
NSDateComponents *days = [[NSDateComponents alloc] init];
days.day = -1 * i;
[result addObject:[_calendar dateByAddingComponents:days toDate:d options:0]];
}
[result addObject:d];
for (NSUInteger i = 0;i<after;i++) {
NSDateComponents *days = [[NSDateComponents alloc] init];
days.day = i+1;
[result addObject:[_calendar dateByAddingComponents:days toDate:d options:0]];
}
return result;
}
#pragma mark - Helpers
- (NSDate*)dateWithoutTimeFromDate:(NSDate*)date {
if (!date) return nil;
NSDateComponents* components = [_calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
return [_calendar dateFromComponents:components];
}
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (NSArray*)weekdayTitlesWithLocaleIdentifier:(NSString*)localeIdentifier length:(NSUInteger)length uppercase:(BOOL)uppercase {
// Get weekday titles from current calendar + locale:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier ? localeIdentifier :[[NSLocale preferredLanguages] firstObject]];
NSMutableArray *wds = [NSMutableArray array];
for (NSString *s in df.shortWeekdaySymbols) {
NSString *clipped = [s substringWithRange:NSMakeRange(0, MIN(length,s.length))];
if (uppercase) {
clipped = [clipped uppercaseStringWithLocale:df.locale];
}
[wds addObject:clipped];
}
// ..finally, normalize so monday is at index 0
return [[wds subarrayWithRange:NSMakeRange(1, 6)]
arrayByAddingObjectsFromArray:[wds subarrayWithRange:NSMakeRange(0,1)]];
}
@end