想要創(chuàng)建指定月的日歷输吏,需要兩個條件:
1.每個月的第一天是周幾(蔡勒公式可得)
2.該月的總天數(shù)(OC的NSCalendar類中的方法可得)
預(yù)覽圖如下:
calendar.png
在輸入框輸入一個合法日期贯溅,如1999年1月1日則輸入19990101,點(diǎn)擊SHOW按鈕即可生成1999年1月的日歷译柏。
在.m文件中實(shí)現(xiàn)以下代碼:
@interface TwoViewController ()
{
UITextField *_inputTF;
UILabel *_monthLabel;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
//輸入框
_inputTF = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, 100, 200, 44)];
_inputTF.keyboardType = UIKeyboardTypeNumberPad;
_inputTF.borderStyle = UITextBorderStyleBezel;
[self.view addSubview:_inputTF];
//按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(CGRectGetMaxX(_inputTF.frame) + 20, 100, 50, 44)];
[btn setTitle:@"SHOW" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(getCalendarSubs) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
_monthLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 150, 200, 44)];
_monthLabel.textAlignment = NSTextAlignmentCenter;
_monthLabel.textColor = [UIColor grayColor];
[self.view addSubview:_monthLabel];
UIView *weekListView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 44)];
NSArray *weekTitleArr = @[@"日",@"一",@"二",@"三",@"四",@"五",@"六"];
for (NSInteger i = 0; i < 7; i++) {
UILabel *weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * (self.view.frame.size.width / 7), 0, self.view.frame.size.width / 7, 44)];
weekLabel.textAlignment = NSTextAlignmentCenter;
weekLabel.text = weekTitleArr[i];
weekLabel.textColor = [UIColor grayColor];
[weekListView addSubview:weekLabel];
}
[self.view addSubview:weekListView];
}
// 計(jì)算每個月的第一天是周幾
- (NSInteger)getWeeks {
//方法一(不推薦⊙﹏⊙‖∣°)
/*
NSInteger c = [_inputTF.text substringWithRange:NSMakeRange(0, 2)].intValue;
NSInteger y = [_inputTF.text substringWithRange:NSMakeRange(2, 2)].intValue;
NSInteger m = [_inputTF.text substringWithRange:NSMakeRange(4, 2)].intValue;
NSInteger d = 1;
_monthLabel.text = [NSString stringWithFormat:@"%ld%ld年%ld月",c,y,m];
//蔡勒公式計(jì)算某天是周幾,不清楚的可以先移步百度或谷歌了解下
//http://baike.baidu.com/link?url=mhB5nRHyMv1GqQRc_2fapQvxihnee8A7urzYMcWCG0_-0ptKBaKYERjdKolaq255tAdKveX_JdSiL_X-ZACLnK
if (m == 1) {
m = 13;
if (y == 0) {
c -= 1;
y = 99;
}
else {
y -= 1;
}
}
if (m == 2) {
m = 14;
if (y == 0) {
c -= 1;
y = 99;
}
else {
y -= 1;
}
}
CGFloat tmp1 = y + (y / 4) + (c / 4) - (2 * c) + (2.6 * (m + 1)) + d - 1;
//取整
NSInteger tmp2 = tmp1 / 1;
NSInteger w = tmp2 % 7;
return w;
*/
//方法2(正規(guī)軍)
NSInteger y = [_inputTF.text substringWithRange:NSMakeRange(0, 4)].intValue;
NSInteger m = [_inputTF.text substringWithRange:NSMakeRange(4, 2)].intValue;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyyMMdd";
NSDate *date = [fmt dateFromString:[NSString stringWithFormat:@"%@%@01",y,m]];
NSDateFormatter* fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"EEEE";
fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString* dateString = [fmt stringFromDate:date];
NSArray *weeks = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"];
for (NSString *week in weeks) {
if ([dateString isEqualToString:week]) {
return [weeks indexOfObject:week];
}
}
return 0;
//方法3
NSInteger y = [_inputTF.text substringWithRange:NSMakeRange(0, 4)].intValue;
NSInteger m = [_inputTF.text substringWithRange:NSMakeRange(4, 2)].intValue;
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyyMMdd";
NSDate *date = [fmt dateFromString:[NSString stringWithFormat:@"%04ld%02ld01",(long)y,(long)m]];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *component = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday) fromDate:date];
return component.weekday-1;
}
//獲取該月總天數(shù)
- (NSInteger)getNumberOfDaysInMonth {
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *component = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
component.year = [_inputTF.text substringWithRange:NSMakeRange(0, 4)].intValue;
component.month = [_inputTF.text substringWithRange:NSMakeRange(4, 2)].intValue;
component.day = 1;
NSDate * date = [calendar dateFromComponents:component];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
return range.length;
}
//創(chuàng)建日歷
- (void)getCalendarSubs {
[_inputTF resignFirstResponder];
//銷毀上次創(chuàng)建的日歷
for (UIView *view in self.view.subviews) {
if (view.frame.size.height > 240) {
[view removeFromSuperview];
}
}
NSInteger daysCount = [self getNumberOfDaysInMonth];
NSInteger firstDayWeek = [self getWeeks];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 250, self.view.frame.size.width, 500)];
for (NSInteger i = 1; i <= daysCount; i++) {
CGFloat X = ((firstDayWeek + i - 1) % 7) * (self.view.frame.size.width / 7);
CGFloat Y = (i + firstDayWeek - 1) / 7 * (self.view.frame.size.width / 7);
CGFloat W = self.view.frame.size.width / 7;
CGFloat H = W;
UILabel *day = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, H)];
day.textAlignment = NSTextAlignmentCenter;
day.textColor = [UIColor grayColor];
day.text = [NSString stringWithFormat:@"%ld",i];
[view addSubview:day];
}
[self.view addSubview:view];
}