diff --git a/RDVCalendarView.podspec b/RDVCalendarView.podspec index 4c2cd87..24eeb52 100644 --- a/RDVCalendarView.podspec +++ b/RDVCalendarView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "RDVCalendarView" - s.version = "1.0.2" + s.version = "1.0.3" s.summary = "Highly customizable calendarView and calendarViewController for iOS" s.description = "RDVCalendarView is iPad and iPhone compatible. Everything can be customized. Supports landscape and portrait orientations and is fully localized using NSLocale." @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { "Robert Dimitrov" => "robert_dimitrov@me.com" } s.platform = :ios, '5.0' - s.source = { :git => "https://github.com/robbdimitrov/RDVCalendarView.git", :tag => "v1.0.2" } + s.source = { :git => "https://github.com/robbdimitrov/RDVCalendarView.git", :tag => "v1.0.3" } s.source_files = 'RDVCalendarView', 'RDVCalendarView/**/*.{h,m}' s.framework = 'UIKit', 'CoreGraphics', 'Foundation' s.requires_arc = true diff --git a/RDVCalendarView/RDVCalendarDayCell.m b/RDVCalendarView/RDVCalendarDayCell.m index d326840..1c7ac44 100644 --- a/RDVCalendarView/RDVCalendarDayCell.m +++ b/RDVCalendarView/RDVCalendarDayCell.m @@ -93,22 +93,27 @@ - (void)setSelected:(BOOL)selected animated:(BOOL)animated { _highlighted = NO; if ([self selectionStyle] != RDVCalendarDayCellSelectionStyleNone) { + __weak RDVCalendarDayCell *weakSelf = self; + void (^block)() = ^{ if (selected) { - [[self backgroundView] setAlpha:0.0f]; - [[self selectedBackgroundView] setAlpha:1.0f]; + [[weakSelf backgroundView] setAlpha:0.0f]; + [[weakSelf selectedBackgroundView] setAlpha:1.0f]; } else { - [[self backgroundView] setAlpha:1.0f]; - [[self selectedBackgroundView] setAlpha:0.0f]; + [[weakSelf backgroundView] setAlpha:1.0f]; + [[weakSelf selectedBackgroundView] setAlpha:0.0f]; + } + for (id subview in [[weakSelf contentView] subviews]) { + if ([subview respondsToSelector:@selector(setHighlighted:)]) { + [subview setHighlighted:selected]; + } } - [[self textLabel] setHighlighted:selected]; }; if (animated) { [UIView animateWithDuration:0.25f animations:block]; } else { block(); - } } } @@ -130,15 +135,21 @@ - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { _selected = NO; if ([self selectionStyle] != RDVCalendarDayCellSelectionStyleNone) { + __weak RDVCalendarDayCell *weakSelf = self; + void (^block)() = ^{ if (highlighted) { - [[self backgroundView] setAlpha:0.0f]; - [[self selectedBackgroundView] setAlpha:1.0f]; + [[weakSelf backgroundView] setAlpha:0.0f]; + [[weakSelf selectedBackgroundView] setAlpha:1.0f]; } else { - [[self backgroundView] setAlpha:1.0f]; - [[self selectedBackgroundView] setAlpha:0.0f]; + [[weakSelf backgroundView] setAlpha:1.0f]; + [[weakSelf selectedBackgroundView] setAlpha:0.0f]; + } + for (id subview in [[weakSelf contentView] subviews]) { + if ([subview respondsToSelector:@selector(setHighlighted:)]) { + [subview setHighlighted:highlighted]; + } } - [[self textLabel] setHighlighted:highlighted]; }; if (animated) { diff --git a/RDVCalendarView/RDVCalendarView.h b/RDVCalendarView/RDVCalendarView.h index 4c04904..0c302d3 100755 --- a/RDVCalendarView/RDVCalendarView.h +++ b/RDVCalendarView/RDVCalendarView.h @@ -157,6 +157,11 @@ typedef NS_ENUM(NSInteger, RDVCalendarViewDayCellSeparatorType) { */ - (RDVCalendarDayCell *)dayCellForIndex:(NSInteger)index; +/** + * Returns the NSDate at the specified index. + */ +- (NSDate *)dateForIndex:(NSInteger)index; + #pragma mark - Managing Selections /** diff --git a/RDVCalendarView/RDVCalendarView.m b/RDVCalendarView/RDVCalendarView.m index 274570c..68954c7 100755 --- a/RDVCalendarView/RDVCalendarView.m +++ b/RDVCalendarView/RDVCalendarView.m @@ -99,7 +99,7 @@ - (id)initWithFrame:(CGRect)frame // Setup calendar - NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; + NSCalendar *calendar = [self calendar]; _currentDay = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:[NSDate date]]; @@ -286,8 +286,18 @@ - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier { #pragma mark - Set up a calendar view +- (NSCalendar *)calendar { + static NSCalendar *calendar = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + calendar = [NSCalendar autoupdatingCurrentCalendar]; + [calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; + }); + return calendar; +} + - (void)setupWeekDays { - NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; + NSCalendar *calendar = [self calendar]; NSInteger firstWeekDay = [calendar firstWeekday] - 1; // We need an NSDateFormatter to have access to the localized weekday strings @@ -390,7 +400,7 @@ - (void)setSelectedDate:(NSDate *)selectedDate { NSDate *oldDate = [self selectedDate]; if (![oldDate isEqualToDate:selectedDate]) { - NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; + NSCalendar *calendar = [self calendar]; if (![self selectedDay]) { [self setSelectedDay:[[NSDateComponents alloc] init]]; @@ -420,7 +430,7 @@ - (void)setSelectedDate:(NSDate *)selectedDate { - (NSDate *)selectedDate { if ([self selectedDay]) { - return [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:[self selectedDay]]; + return [[self calendar] dateFromComponents:[self selectedDay]]; } return nil; } @@ -445,21 +455,23 @@ - (RDVCalendarDayCell *)dayCellForIndex:(NSInteger)index { } } - [dayCell prepareForReuse]; - [dayCell.textLabel setText:[NSString stringWithFormat:@"%d", index + 1]]; - - if (index + 1 == [self currentDay].day && - [self month].month == [self currentDay].month && - [self month].year == [self currentDay].year) { + if (![[self visibleCells] containsObject:dayCell]) { + [dayCell prepareForReuse]; + [dayCell.textLabel setText:[NSString stringWithFormat:@"%d", index + 1]]; + + if (index + 1 == [self currentDay].day && + [self month].month == [self currentDay].month && + [self month].year == [self currentDay].year) { [[dayCell backgroundView] setBackgroundColor:[self currentDayColor]]; - } else { - [[dayCell backgroundView] setBackgroundColor:[self normalDayColor]]; + } else { + [[dayCell backgroundView] setBackgroundColor:[self normalDayColor]]; + } + + [[dayCell selectedBackgroundView] setBackgroundColor:[self selectedDayColor]]; + + [dayCell setNeedsLayout]; } - [[dayCell selectedBackgroundView] setBackgroundColor:[self selectedDayColor]]; - - [dayCell setNeedsLayout]; - return dayCell; } @@ -477,6 +489,17 @@ - (NSInteger)indexForDayCellAtPoint:(CGPoint)point { return 0; } +- (NSDate *)dateForIndex:(NSInteger)index { + NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; + [dateComponents setYear:[[self month] year]]; + [dateComponents setMonth:[[self month] month]]; + [dateComponents setDay:(index + 1)]; + + NSDate *date = [[self calendar] dateFromComponents:dateComponents]; + + return date; +} + #pragma mark - Managing selections - (NSInteger)indexForSelectedDayCell { @@ -496,10 +519,10 @@ - (void)selectDayCellAtIndex:(NSInteger)index animated:(BOOL)animated { NSDateComponents *selectedDayComponents = [[NSDateComponents alloc] init]; [selectedDayComponents setMonth:[[self month] month]]; [selectedDayComponents setYear:[[self month] year]]; - [selectedDayComponents setDay:index + 1]; + [selectedDayComponents setDay:(index + 1)]; if ([[self delegate] respondsToSelector:@selector(calendarView:willSelectDate:)]) { - [[self delegate] calendarView:self willSelectDate:[[[self month] calendar] + [[self delegate] calendarView:self willSelectDate:[[self calendar] dateFromComponents:selectedDayComponents]]; } @@ -513,7 +536,7 @@ - (void)selectDayCellAtIndex:(NSInteger)index animated:(BOOL)animated { [self setSelectedDay:selectedDayComponents]; if ([[self delegate] respondsToSelector:@selector(calendarView:didSelectDate:)]) { - [[self delegate] calendarView:self didSelectDate:[[[self month] calendar] + [[self delegate] calendarView:self didSelectDate:[[self calendar] dateFromComponents:[self selectedDay]]]; } @@ -540,21 +563,21 @@ - (void)deselectDayCellAtIndex:(NSInteger)index animated:(BOOL)animated { #pragma mark - Helper methods - (NSInteger)numberOfWeeks { - return [[[self month] calendar] rangeOfUnit:NSDayCalendarUnit - inUnit:NSWeekCalendarUnit - forDate:[self firstDay]].length; + return [[self calendar] rangeOfUnit:NSDayCalendarUnit + inUnit:NSWeekCalendarUnit + forDate:[self firstDay]].length; } - (NSInteger)numberOfDays { - return [[[self month] calendar] rangeOfUnit:NSDayCalendarUnit - inUnit:NSMonthCalendarUnit - forDate:[self firstDay]].length; + return [[self calendar] rangeOfUnit:NSDayCalendarUnit + inUnit:NSMonthCalendarUnit + forDate:[self firstDay]].length; } - (NSInteger)numberOfDaysInFirstWeek { - return [[[self month] calendar] rangeOfUnit:NSDayCalendarUnit - inUnit:NSWeekCalendarUnit - forDate:[self firstDay]].length; + return [[self calendar] rangeOfUnit:NSDayCalendarUnit + inUnit:NSWeekCalendarUnit + forDate:[self firstDay]].length; } - (RDVCalendarDayCell *)viewAtLocation:(CGPoint)location { @@ -605,6 +628,7 @@ - (void)showNextMonth { - (void)currentLocaleDidChange:(NSNotification *)notification { [self setupWeekDays]; [self updateMonthLabelMonth:[self month]]; + [self setNeedsLayout]; } #pragma mark - Touch handling