第一次接觸到日歷的開發(fā)扎谎,表示需要學(xué)習(xí)的東西還有很多呢碳想!
關(guān)于日歷的開發(fā)烧董,如果不進(jìn)行相關(guān)設(shè)置的話,默認(rèn)是沒有農(nóng)歷的胧奔,需要我們進(jìn)行設(shè)置逊移。
核心Demo如下:
monthArr = [NSArray arrayWithObjects:
@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
@"九月", @"十月", @"冬月", @"臘月", nil];
dayArr = [NSArray arrayWithObjects:
@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
unsigned unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
NSString *monthStr = [monthArr objectAtIndex:localeComp.month-1];
NSString *dayString = [dayArr objectAtIndex:localeComp.day-1];
當(dāng)然如果你需要顯示當(dāng)前年份的農(nóng)歷表示需要新增如下設(shè)置:
NSArray *chineseYears = [NSArray arrayWithObjects:
@"甲子", @"乙丑", @"丙寅", @"丁卯", @"戊辰", @"己巳", @"庚午", @"辛未", @"壬申", @"癸酉",
@"甲戌", @"乙亥", @"丙子", @"丁丑", @"戊寅", @"己卯", @"庚辰", @"辛己", @"壬午", @"癸未",
@"甲申", @"乙酉", @"丙戌", @"丁亥", @"戊子", @"己丑", @"庚寅", @"辛卯", @"壬辰", @"癸巳",
@"甲午", @"乙未", @"丙申", @"丁酉", @"戊戌", @"己亥", @"庚子", @"辛丑", @"壬寅", @"癸丑",
@"甲辰", @"乙巳", @"丙午", @"丁未", @"戊申", @"己酉", @"庚戌", @"辛亥", @"壬子", @"癸丑",
@"甲寅", @"乙卯", @"丙辰", @"丁巳", @"戊午", @"己未", @"庚申", @"辛酉", @"壬戌", @"癸亥", nil];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSString *yearStr = [chineseYears objectAtIndex:localeComp.year-1];
效果圖如下:
Snip20160509_20.png
GitHub上第三方日歷有兩個(gè)可以試著使用一下:
1、FSCalendar
2龙填、JTCalendar
個(gè)人推薦第一個(gè)胳泉,F(xiàn)SCalendar封裝得比較完善。
mark --->關(guān)于代碼放在什么位置FSCalendar中DataSource
有一個(gè)方法
Demo如下:
#pragma mark - FSCalendarDataSource
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date
{
monthArr = [NSArray arrayWithObjects:
@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
@"九月", @"十月", @"冬月", @"臘月", nil];
dayArr = [NSArray arrayWithObjects:
@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
@"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
unsigned unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
NSString *monthStr = [monthArr objectAtIndex:localeComp.month-1];
NSString *dayString = [dayArr objectAtIndex:localeComp.day-1];
NSString *chineseCal_str;
if ([dayString isEqualToString:@"初一"]) {
chineseCal_str = monthStr;
} else {
chineseCal_str = dayString;
}
return chineseCal_str;
}
其他諸如 JTCalendar 框架中如何設(shè)置農(nóng)歷顯示岩遗,可在其DataSource
或者Delegate
中查看是否有 subtitleForDate
類似的設(shè)置