iOS 日歷 NSCalendar

NSCalendar 是 iOS 獲取日歷數(shù)據(jù)的工具類。

獲取指定月份的天數(shù):

// 獲取當(dāng)月的天數(shù)
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
// 天數(shù)
self.daysOfMouth = range.length;

獲取指定日期的是周幾,在 NSCalendar 中,默認(rèn)周日為每周的第一天,序號為 1,周一為 2,以此類推:

// 獲取今天是周幾
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:[NSDate date]];
// 默認(rèn)周日為每周的第一天,序號為1,周一為2,以此類推
NSInteger firstDay = [comps weekday];

根據(jù)產(chǎn)品需求,設(shè)計如下的月份 UI:

QQ20180328-185206@2x.png

使用 UICollectionView 來展示月份。
聲明全局變量:

/**
 當(dāng)前月份
 */
@property (weak, nonatomic) UILabel *labCurrentMouth;

/**
 日歷網(wǎng)格
 */
@property (weak, nonatomic) UICollectionView *collectCalendar;

/**
 本月有多少天
 */
@property (assign, nonatomic) NSInteger daysOfMouth;

/**
 每個月第一行空白的天數(shù)
 */
@property (assign, nonatomic) NSInteger numBlank;

UICollectionView 布局:

    // 月份日歷
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 7.5, 0, 7.5);
    flowLayout.minimumLineSpacing = 2;
    flowLayout.minimumInteritemSpacing = 15;
    flowLayout.itemSize = CGSizeMake(25, 25);
    
    UICollectionView *collectTemp = [[UICollectionView alloc] initWithFrame:CGRectMake(widthEdge, 82, 294, 168) collectionViewLayout:flowLayout];
    [collectTemp setBackgroundColor:kColor_C6];
    collectTemp.delegate = self;
    collectTemp.dataSource = self;
    [checkInView addSubview:collectTemp];
    self.collectCalendar = collectTemp;
    
    // 注冊 cell
    [self.collectCalendar registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:gDayID];

因為星期的排版是固定的,所以月份需要根據(jù)第一天是周幾來空出相應(yīng)的空白天數(shù),以便和星期相對應(yīng)。
月份布局的相關(guān)計算:

   // 當(dāng)月時間戳
    NSDate * currentDate = [NSDate date];
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-M"];
    NSString *strCurrent = [formatter stringFromDate:currentDate];
    [self.labCurrentMouth setText:[strCurrent stringByAppendingFormat:@"月"]];
    
    // 計算月份的第一天為周幾
    // 獲取每月的第一天
    NSString * strFistDay = [NSString stringWithFormat:@"%@-1", strCurrent];
    [formatter setDateFormat:@"yyyy-M-dd"];
    NSDate *date = [formatter dateFromString:strFistDay];
    
    // 日歷中,默認(rèn)周日一天,序號為1,周一為2,以此類推
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:date];
    NSInteger firstDay = [comps weekday];
    
    // 因為產(chǎn)品設(shè)計周一為第一天,所以要做相應(yīng)的調(diào)整
    // 每個月第一行空白的天數(shù)
    if (firstDay == 1) {
        
        // 第一天是周日
        self.numBlank = 6;
    } else {
        
        self.numBlank = firstDay - 2;
    }

    
    // 獲取當(dāng)月的天數(shù)
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];
    self.daysOfMouth = range.length;

UICollectionViewDelegateUICollectionViewDataSource


#pragma mark - UICollectionDataSource
// 單元數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    // 月份排版的空白加上月份的總天數(shù)
    return self.numBlank + self.daysOfMouth;
}

// cell 定義
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:gDayID forIndexPath:indexPath];
    
    // 月份的號數(shù)
    UILabel *labDay = (UILabel *)[cell viewWithTag:10];
    
    if (labDay == nil) {
        
        labDay = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
        [labDay setTag:10];
        [labDay setClipsToBounds:YES];
        [labDay.layer setCornerRadius:labDay.frame.size.height/2];
        [labDay setTextAlignment:NSTextAlignmentCenter];
        [cell addSubview:labDay];
        
        
        [labDay setBackgroundColor:kColor_C1];
        [labDay setFont:kFont_h3];
        [labDay setTextColor:kColor_C6];
    }
    
    // 隱藏空白、顯示號數(shù) 
    if (indexPath.row < self.numBlank) {

        [labDay setHidden:YES];
    } else {
        
        [labDay setHidden:NO];
        [labDay setText:[NSString stringWithFormat:@"%ld", (long)indexPath.row - self.numBlank + 1]];
    }
    
    return cell;
}

完成!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容