Skip to content

Instantly share code, notes, and snippets.

@nihilenz
Created August 19, 2010 10:58
Show Gist options
  • Save nihilenz/537603 to your computer and use it in GitHub Desktop.
Save nihilenz/537603 to your computer and use it in GitHub Desktop.
- (id) initWithMonth:(NSDate*)date marks:(NSArray*)markArray startDayOnSunday:(BOOL)sunday{
firstOfPrev = -1;
marks = [markArray retain];
monthDate = [date retain];
startOnSunday = sunday;
TKDateInformation dateInfo = [monthDate dateInformation];
firstWeekday = dateInfo.weekday;
daysInMonth = [date daysInMonth];
TKDateInformation todayInfo = [[NSDate date] dateInformation];
today = dateInfo.month == todayInfo.month && dateInfo.year == todayInfo.year ? todayInfo.day : 0;
if((sunday && firstWeekday>1) || (!sunday && firstWeekday!=2)){
dateInfo.month--;
if(dateInfo.month<1) {
dateInfo.month = 12;
dateInfo.year--;
}
NSDate *previousMonth = [NSDate dateFromDateInformation:dateInfo];
int preDayCnt = [previousMonth daysInMonth];
firstOfPrev = preDayCnt - firstWeekday;
if (sunday) {
firstOfPrev += 2;
} else {
if (firstWeekday == 1) {
firstOfPrev -= 4;
} else {
firstOfPrev += 3;
}
}
lastOfPrev = preDayCnt;
}
int prevDays = (firstOfPrev == -1 ? 0 : lastOfPrev - firstOfPrev + 1);
int row = daysInMonth + prevDays;
row = (row / 7) + ((row % 7 == 0) ? 0:1);
float h = 44 * row;
if(![super initWithFrame:CGRectMake(0, 1, 320, h+1)]) return nil;
[self.selectedImageView addSubview:self.currentDay];
[self.selectedImageView addSubview:self.dot];
self.multipleTouchEnabled = NO;
return self;
}
+ (NSArray*) rangeOfDatesInMonthGrid:(NSDate*)date startOnSunday:(BOOL)sunday{
NSDate *firstDate, *lastDate;
TKDateInformation info = [date dateInformation];
info.day = 1;
NSDate *d = [NSDate dateFromDateInformation:info];
info = [d dateInformation];
if((sunday && info.weekday>1) || (!sunday && info.weekday!=2)){
TKDateInformation info2 = info;
info2.month--;
if(info2.month<1) { info2.month = 12; info2.year--; }
NSDate *previousMonth = [NSDate dateFromDateInformation:info2];
int preDayCnt = [previousMonth daysInMonth];
info2.day = preDayCnt - info.weekday;
if (sunday) {
info2.day += 2;
} else {
if (info.weekday == 1) {
info2.day -= 4;
} else {
info2.day += 3;
}
}
firstDate = [NSDate dateFromDateInformation:info2];
}else{
firstDate = d;
}
int daysInMonth = [d daysInMonth];
info.day = daysInMonth;
NSDate *lastInMonth = [NSDate dateFromDateInformation:info];
info = [lastInMonth dateInformation];
if((sunday && info.weekday < 7) || (!sunday && info.weekday != 1)){
if (sunday) {
info.day = 7 - info.weekday;
}
else {
info.day = 7 - info.weekday + 1;
}
info.month++;
if(info.month>12){
info.month = 1;
info.year++;
}
lastDate = [NSDate dateFromDateInformation:info];
}else{
lastDate = lastInMonth;
}
return [NSArray arrayWithObjects:firstDate,lastDate,nil];
}
[...]
if(row == (int) (self.bounds.size.height / 44)) row --;
// beginning of edit
if (startOnSunday) {
if(row==0 && column < firstWeekday-1){
day = firstOfPrev + column;
} else {
portion = 1;
day = row * 7 + column - firstWeekday+2;
}
}
else {
if(row==0 && column < firstWeekday-2){
day = firstOfPrev + column;
} else {
portion = 1;
if (firstWeekday == 1) {
day = row * 7 + column - firstWeekday - 4;
}
else {
day = row * 7 + column - firstWeekday + 3;
}
}
}
if(portion > 0 && day > daysInMonth){
portion = 2;
day = day - daysInMonth;
}
// end of edit
if(portion != 1){
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment