iOS自定义显示30天日历

#pragma mark - 

#pragma mark 自定义日历表



- (void)createDefineCalander {

    [self getCurrentDays];

    [self getNextDays];

    _MsDateTimes = [NSMutableArray new];

    CGFloat buttonWidth = width(_calanderView.frame) / 7;

    int DAY_OF_MONTH = 30;

    NSInteger row = 0;

    NSInteger flag = 1;

    NSInteger k = 1;

    NSInteger j = 1;

    for (NSInteger i = _currentWeekday,index = _currentWeekday - 1,curDay = _currentNumberOfDaysInMonth ; i < DAY_OF_MONTH + index; i++) {

         UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];

        if (i%7 == 0) {

            itemButton.frame = CGRectMake(index*buttonWidth, row*buttonWidth + buttonWidth, buttonWidth, buttonWidth);

            row++;

        }else {

            itemButton.frame = CGRectMake(index*buttonWidth, row*buttonWidth + buttonWidth, buttonWidth, buttonWidth);

        }

        [itemButton setTitleColor:LHColor(108, 108, 108) forState:UIControlStateNormal];

        itemButton.tag = 100+k;

        itemButton.layer.cornerRadius = buttonWidth/2.0;

        itemButton.clipsToBounds = YES;

        [itemButton addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];

        if(i == _currentWeekday) {

            [itemButton setTitle:@"今天" forState:UIControlStateNormal];

        } else {

            [itemButton setTitle:[NSString stringWithFormat:@"%ld",curDay] forState:UIControlStateNormal];

        }

        NSInteger monthLenth = _currentNumberOfDaysInMonth;

        if(flag == 1) {

            monthLenth = _currentNumberOfDaysInMonth;

        } else if(flag == 2) {

            monthLenth = _nextNumberOfDaysInMonth;

        }

        if(curDay == monthLenth) {

            curDay = 1;

            flag = 2;

        } else {

            curDay++;

        }

        if(i % 7 == 0) {

            index = 0;

            j++;

        } else {

            index++;

        }

        k++;

        [_calanderView addSubview:itemButton];

        _calanderView.frame =CGRectMake(0, 46, width(_backGroundView.frame), j*buttonWidth);

    }

}

你可能感兴趣的:(日历)