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

Honor locale settings #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions MNCalendarView/MNCalendarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ - (void)setCalendar:(NSCalendar *)calendar {
_calendar = calendar;

self.monthFormatter = [[NSDateFormatter alloc] init];
self.monthFormatter.locale = [calendar locale];
self.monthFormatter.calendar = calendar;
[self.monthFormatter setDateFormat:@"MMMM yyyy"];
}
Expand All @@ -119,7 +120,8 @@ - (void)reloadData {

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = self.calendar;

formatter.locale = self.calendar.locale;

self.weekdaySymbols = formatter.shortWeekdaySymbols;

[self.collectionView reloadData];
Expand All @@ -140,24 +142,15 @@ - (void)registerUICollectionViewClasses {
- (NSDate *)firstVisibleDateOfMonth:(NSDate *)date {
date = [date mn_firstDateOfMonth:self.calendar];

NSDateComponents *components =
[self.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
fromDate:date];

return
[[date mn_dateWithDay:-((components.weekday - 1) % self.daysInWeek) calendar:self.calendar] dateByAddingTimeInterval:MN_DAY];
NSDateComponents *components = [self.calendar components:NSWeekdayCalendarUnit fromDate:date];
return [date dateByAddingTimeInterval:-MN_DAY * ((components.weekday - self.calendar.firstWeekday + self.daysInWeek) % self.daysInWeek)];
}

- (NSDate *)lastVisibleDateOfMonth:(NSDate *)date {
date = [date mn_lastDateOfMonth:self.calendar];

NSDateComponents *components =
[self.calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
fromDate:date];

return
[date mn_dateWithDay:components.day + (self.daysInWeek - 1) - ((components.weekday - 1) % self.daysInWeek)
calendar:self.calendar];

NSDateComponents *components = [self.calendar components:NSWeekdayCalendarUnit fromDate:date];
return [date dateByAddingTimeInterval:MN_DAY * (self.daysInWeek - 1 - ((components.weekday - self.calendar.firstWeekday + self.daysInWeek) % self.daysInWeek))];
}

- (void)applyConstraints {
Expand Down Expand Up @@ -238,7 +231,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
forIndexPath:indexPath];

cell.backgroundColor = self.collectionView.backgroundColor;
cell.titleLabel.text = self.weekdaySymbols[indexPath.item];
cell.titleLabel.text = self.weekdaySymbols[(NSUInteger) ((indexPath.item + self.calendar.firstWeekday - 1) % self.daysInWeek)];
cell.separatorColor = self.separatorColor;
return cell;
}
Expand All @@ -250,7 +243,7 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
NSDate *monthDate = self.monthDates[indexPath.section];
NSDate *firstDateInMonth = [self firstVisibleDateOfMonth:monthDate];

NSUInteger day = indexPath.item - self.daysInWeek;
NSInteger day = indexPath.item - self.daysInWeek;

NSDateComponents *components =
[self.calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit
Expand Down Expand Up @@ -307,10 +300,10 @@ - (CGSize)collectionView:(UICollectionView *)collectionView
CGFloat itemWidth = roundf(width / self.daysInWeek);
CGFloat itemHeight = indexPath.item < self.daysInWeek ? 30.f : itemWidth;

NSUInteger weekday = indexPath.item % self.daysInWeek;
NSInteger weekday = (indexPath.item + self.calendar.firstWeekday - 1) % self.daysInWeek;

if (weekday == self.daysInWeek - 1) {
itemWidth = width - (itemWidth * (self.daysInWeek - 1));
itemWidth = width - itemWidth * (self.daysInWeek - 1); // use all remaining width for rightmost element
}

return CGSizeMake(itemWidth, itemHeight);
Expand Down
35 changes: 20 additions & 15 deletions MNCalendarView/MNCalendarViewDayCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface MNCalendarViewDayCell()
@property(nonatomic,strong,readwrite) NSDate *date;
@property(nonatomic,strong,readwrite) NSDate *month;
@property(nonatomic,assign,readwrite) NSUInteger weekday;
@property(nonatomic,assign, readwrite) BOOL withinMonth;

@end

Expand All @@ -36,21 +37,24 @@ - (void)setDate:(NSDate *)date
[self.calendar components:NSMonthCalendarUnit
fromDate:self.month];

self.weekday = components.weekday;
self.titleLabel.text = [NSString stringWithFormat:@"%d", components.day];
self.enabled = monthComponents.month == components.month;

self.weekday = (NSUInteger) components.weekday;
self.withinMonth = (monthComponents.month == components.month);
self.enabled = self.withinMonth;
self.titleLabel.text = self.withinMonth ? [@(components.day) stringValue] : @"";

[self setNeedsDisplay];
}

- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
[super setEnabled:(enabled && self.withinMonth)];

self.titleLabel.textColor =
self.enabled ? UIColor.darkTextColor : UIColor.lightGrayColor;

self.backgroundColor =
self.enabled ? UIColor.whiteColor : [UIColor colorWithRed:.96f green:.96f blue:.96f alpha:1.f];
self.enabled ? UIColor.whiteColor :
self.withinMonth ? [UIColor colorWithRed:.96f green:.96f blue:.96f alpha:1.f] :
self.separatorColor;
}

- (void)drawRect:(CGRect)rect {
Expand All @@ -61,14 +65,15 @@ - (void)drawRect:(CGRect)rect {
CGColorRef separatorColor = self.separatorColor.CGColor;

CGSize size = self.bounds.size;

if (self.weekday != 7) {
CGFloat pixel = 1.f / [UIScreen mainScreen].scale;
MNContextDrawLine(context,
CGPointMake(size.width - pixel, pixel),
CGPointMake(size.width - pixel, size.height),
separatorColor,
pixel);

if (self.withinMonth && self.weekday != self.calendar.firstWeekday) {
CGFloat pixel = 1.f / [UIScreen mainScreen].scale;
MNContextDrawLine(context,
CGPointMake(0, 0),
CGPointMake(0, size.height),
separatorColor,
pixel);

}
}

Expand Down