NSCalendar 是 iOS 獲取日歷數(shù)據(jù)的工具類喜命。
獲取指定月份的天數(shù):
// 獲取當月的天數(shù)
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
// 天數(shù)
self.daysOfMouth = range.length;
獲取指定日期的是周幾,在 NSCalendar
中吃靠,默認周日為每周的第一天硫眨,序號為 1
足淆,周一為 2
巢块,以此類推:
// 獲取今天是周幾
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:[NSDate date]];
// 默認周日為每周的第一天,序號為1巧号,周一為2族奢,以此類推
NSInteger firstDay = [comps weekday];
根據(jù)產(chǎn)品需求,設(shè)計如下的月份 UI
:
QQ20180328-185206@2x.png
使用 UICollectionView
來展示月份丹鸿。
聲明全局變量:
/**
當前月份
*/
@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)計算:
// 當月時間戳
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];
// 日歷中廊敌,默認周日一天,序號為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;
}
// 獲取當月的天數(shù)
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];
self.daysOfMouth = range.length;
UICollectionViewDelegate
和 UICollectionViewDataSource
:
#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;
}
完成肋殴!