实用Demo00『 基础知识 』iOS收藏

iOS:两个条件生成某月日历

2016-08-04  本文已影响108人  怪客半

想要创建指定月的日历,需要两个条件:
1.每个月的第一天是周几(蔡勒公式可得)
2.该月的总天数(OC的NSCalendar类中的方法可得)

预览图如下:


calendar.png

在输入框输入一个合法日期,如1999年1月1日则输入19990101,点击SHOW按钮即可生成1999年1月的日历。

在.m文件中实现以下代码:

@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];
}
// 计算每个月的第一天是周几
- (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];
    
    //蔡勒公式计算某天是周几,不清楚的可以先移步百度或谷歌了解下
    //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(正规军)
    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;
}
//获取该月总天数
- (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;
}
//创建日历
- (void)getCalendarSubs {
    [_inputTF resignFirstResponder];
    
    //销毁上次创建的日历
    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];
}
上一篇 下一篇

猜你喜欢

热点阅读