我们的自定义日历必须与NSLocale一起使用,因此根据此区域设置更新为从星期一或星期日开始。日历是一个充满按钮的自定义网格。
我将显示的实际天数减去1,这看起来很好,但这些日子本身就是一个真正的痛苦转移。
我想将数字的数字显示转移到正确的日期,具体取决于区域设置。
所以,假设它是2015年2月,如果星期一是一周的开始,那么第一个星期日应该只显示1,然后星期一是2。
然而,我无法让我的按钮尊重这一事实,并最终错误地显示不同日子的日子。
---编辑---
我现在快到了。我得到的东西接近我的期望,但第一天似乎永远不会显示。
请注意,这是在我审核其他建议之前(下方)
NSLocale *autoUpdating = [self getLocale];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:autoUpdating];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.month = self.currentMonth.month;
components.year = self.currentMonth.year;
components.day = 1;
[components setWeekday:[calendar firstWeekday]];
NSDate *firstDay = [calendar dateFromComponents:components];
NSCalendarUnit units = (NSWeekdayCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit);
NSDateComponents *dateComponents = [calendar components:units fromDate:firstDay];
NSDateComponents *todayComponents = [calendar components:(NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear) fromDate:[NSDate date]];
NSDate *today = [calendar dateFromComponents:todayComponents];
__unused NSDate *minimumDate = self.minimumDate ?: today;
NSDate *selected = nil;
if (self.selectedDate) {
selected = [calendar dateFromComponents:self.selectedDate];
}
NSUInteger startDay = dateComponents.weekday;
NSUInteger currentDay = calendar.firstWeekday;
NSRange dayRange = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDay];
for (UIButton *button in self.dayButtons) {
button.selected = NO;
if ( ((button.tag + 1) >= startDay) && (currentDay <= dayRange.length) ) {
NSString *title = [NSString stringWithFormat:@"%@", @(currentDay)];
[button setTitle:title forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateDisabled];
dateComponents.day = currentDay;
NSDate *date = [calendar dateFromComponents:dateComponents];
if (selected && [date compare:selected] == NSOrderedSame) {
button.selected = YES;
}
if ([date compare:minimumDate] == NSOrderedAscending) {
button.enabled = NO;
} else {
button.enabled = YES;
}
currentDay++;
} else {
[button setTitle:@"" forState:UIControlStateNormal];
[button setTitle:@"" forState:UIControlStateDisabled];
}
我所包含的图片显示它对于2月是不正确的,我不知道如何解决它。