DayCell.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // DayCell.m
  3. // DFCalendar
  4. //
  5. // Created by Macsyf on 16/12/7.
  6. // Copyright © 2016年 ZhouDeFa. All rights reserved.
  7. //
  8. #import "DayCell.h"
  9. #define NormalColor [UIColor whiteColor]
  10. #define StartAndEndColor [UIColor redColor]
  11. #define SelectedColor [UIColor orangeColor]
  12. @interface DayCell ()
  13. @property (weak, nonatomic) IBOutlet UIView *colorView;
  14. @property (weak, nonatomic) IBOutlet UILabel *dayLabel;
  15. @property (weak, nonatomic) IBOutlet UILabel *actionLabel;
  16. @property (nonatomic,strong)DayModel *model;
  17. @end
  18. @implementation DayCell
  19. -(void)fullCellWithModel:(DayModel *)model
  20. {
  21. _model = model;
  22. if (model == nil) {
  23. self.dayLabel.text = @"";
  24. self.colorView.backgroundColor = NormalColor;
  25. }
  26. if (model.day) {
  27. self.dayLabel.text = [NSString stringWithFormat:@"%02ld",(long)model.day];
  28. }else{
  29. self.dayLabel.text = @"";
  30. }
  31. switch (model.state) {
  32. case DayModelStateNormal:
  33. {
  34. self.colorView.backgroundColor = NormalColor;
  35. self.dayLabel.textColor = [UIColor blackColor];
  36. self.actionLabel.text = [self lunarWith:model.dayDate];
  37. NSArray *arr = @[@"除夕",@"春节",@"元宵节",@"端午节",@"七夕节",@"中元节",@"中秋节",@"重阳节",@"腊八节",@"北方小年",@"南方小年"];
  38. BOOL isJieRi = NO;
  39. for (NSString *str in arr) {
  40. if ([self.actionLabel.text isEqualToString:str]) {
  41. isJieRi = YES;
  42. break;
  43. }
  44. }
  45. if (isJieRi) {
  46. self.actionLabel.textColor = [UIColor colorWithRed:65/255.0 green:207/255.0 blue:246/255.0 alpha:1];
  47. }else{
  48. self.actionLabel.textColor = [UIColor blackColor];
  49. }
  50. NSString *jieRi = [self getJieJiaRiWith:model.dayDate string:self.actionLabel.text];
  51. if ([jieRi isEqualToString:self.actionLabel.text]) {
  52. // self.actionLabel.textColor = [UIColor blackColor];
  53. }else{
  54. self.actionLabel.textColor = [UIColor colorWithRed:65/255.0 green:207/255.0 blue:246/255.0 alpha:1];
  55. }
  56. self.actionLabel.text = jieRi;
  57. break;
  58. }
  59. case DayModelStateStart:
  60. self.colorView.backgroundColor = StartAndEndColor;
  61. self.dayLabel.textColor = [UIColor whiteColor];
  62. self.actionLabel.text = @"开始";
  63. self.actionLabel.textColor = [UIColor whiteColor];
  64. break;
  65. case DayModelStateEnd:
  66. self.colorView.backgroundColor = StartAndEndColor;
  67. self.dayLabel.textColor = [UIColor whiteColor];
  68. self.actionLabel.text = @"截止";
  69. self.actionLabel.textColor = [UIColor whiteColor];
  70. break;
  71. case DayModelStateSelected:
  72. {
  73. self.colorView.backgroundColor = SelectedColor;
  74. self.dayLabel.textColor = [UIColor whiteColor];
  75. self.actionLabel.text = [self lunarWith:model.dayDate];
  76. // NSArray *arr = @[@"除夕",@"春节",@"元宵节",@"端午节",@"七夕节",@"中元节",@"中秋节",@"重阳节",@"腊八节",@"北方小年",@"南方小年"];
  77. // BOOL isJieRi = NO;
  78. // for (NSString *str in arr) {
  79. // if ([self.actionLabel.text isEqualToString:str]) {
  80. // isJieRi = YES;
  81. // break;
  82. // }
  83. // }
  84. self.actionLabel.textColor = [UIColor whiteColor];
  85. // if (isJieRi) {
  86. // self.actionLabel.textColor = [UIColor colorWithRed:65/255.0 green:207/255.0 blue:246/255.0 alpha:1];
  87. // }else{
  88. // self.actionLabel.textColor = [UIColor whiteColor];
  89. // }
  90. NSString *jieRi = [self getJieJiaRiWith:model.dayDate string:self.actionLabel.text];
  91. // if ([jieRi isEqualToString:self.actionLabel.text]) {
  92. //// self.actionLabel.textColor = [UIColor blackColor];
  93. // }else{
  94. // self.actionLabel.textColor = [UIColor colorWithRed:0/255.0 green:207/255.0 blue:246/255.0 alpha:1];
  95. // }
  96. self.actionLabel.text = jieRi;
  97. break;
  98. }
  99. default:
  100. break;
  101. }
  102. }
  103. -(NSString *)getJieJiaRiWith:(NSDate *)date string:(NSString *)string
  104. {
  105. if (date == nil) {
  106. return string;
  107. }
  108. NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区
  109. NSTimeInterval time = [zone secondsFromGMTForDate:date];//以秒为单位返回当前时间与系统格林尼治时间的差
  110. NSDate *toDayDate = [date dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间
  111. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] ;
  112. NSDateComponents *comps = [[NSDateComponents alloc] init];
  113. NSInteger unitFlags = NSCalendarUnitYear |
  114. NSCalendarUnitMonth |
  115. NSCalendarUnitDay |
  116. NSCalendarUnitWeekday |
  117. NSCalendarUnitHour |
  118. NSCalendarUnitMinute |
  119. NSCalendarUnitSecond;
  120. comps = [calendar components:unitFlags fromDate:toDayDate];
  121. // NSInteger year=[comps year];
  122. NSInteger month = [comps month];
  123. NSInteger day = [comps day];
  124. if (month == 1 && day == 1) {
  125. return @"元旦节";
  126. }
  127. if (month == 2 && day == 14) {
  128. return @"情人节";
  129. }
  130. if (month == 3 && day == 8) {
  131. return @"妇女节";
  132. }
  133. if (month == 3 && day == 12) {
  134. return @"植树节";
  135. }
  136. if (month == 4 && day == 1) {
  137. return @"愚人节";
  138. }
  139. if (month == 4 && day == 5) {
  140. return @"清明节";
  141. }
  142. if (month == 5 && day == 1) {
  143. return @"劳动节";
  144. }
  145. if (month == 5 && day == 4) {
  146. return @"青年节";
  147. }
  148. if (month == 5 && day == 12) {
  149. return @"护士节";
  150. }
  151. if (month == 6 && day == 1) {
  152. return @"儿童节";
  153. }
  154. if (month == 7 && day == 1) {
  155. return @"建党节";
  156. }
  157. if (month == 8 && day == 1) {
  158. return @"建军节";
  159. }
  160. if (month == 9 && day == 10) {
  161. return @"教师节";
  162. }
  163. if (month == 10 && day == 1) {
  164. return @"国庆节";
  165. }
  166. if (month == 11 && day == 11) {
  167. return @"光棍节";
  168. }
  169. if (month == 12 && day == 24) {
  170. return @"平安夜";
  171. }
  172. if (month == 12 && day == 25) {
  173. return @"圣诞节";
  174. }
  175. return string;
  176. }
  177. -(NSString *)lunarWith:(NSDate *)date
  178. {
  179. // NSLog(@"%@",date);
  180. if (date == nil) {
  181. return @"";
  182. }
  183. NSTimeZone *zone = [NSTimeZone systemTimeZone]; //获得系统的时区
  184. NSTimeInterval time = [zone secondsFromGMTForDate:date];//以秒为单位返回当前时间与系统格林尼治时间的差
  185. NSDate *toDayDate = [date dateByAddingTimeInterval:time];//然后把差的时间加上,就是当前系统准确的时间
  186. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian] ;
  187. NSDateComponents *comps = [[NSDateComponents alloc] init];
  188. NSInteger unitFlags = NSCalendarUnitYear |
  189. NSCalendarUnitMonth |
  190. NSCalendarUnitDay |
  191. NSCalendarUnitWeekday |
  192. NSCalendarUnitHour |
  193. NSCalendarUnitMinute |
  194. NSCalendarUnitSecond;
  195. comps = [calendar components:unitFlags fromDate:toDayDate];
  196. NSInteger year=[comps year];
  197. NSInteger month = [comps month];
  198. NSInteger day = [comps day];
  199. NSDictionary *lunarDic = [self LunarForSolarYear:(int )year Month:(int )month Day:(int )day];
  200. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"腊月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"三十"]) {
  201. return @"除夕";
  202. }
  203. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"正月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"初一"]) {
  204. return @"春节";
  205. }
  206. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"正月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"十五"]) {
  207. return @"元宵节";
  208. }
  209. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"五月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"初五"]) {
  210. return @"端午节";
  211. }
  212. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"七月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"初七"]) {
  213. return @"七夕节";
  214. }
  215. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"七月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"十五"]) {
  216. return @"中元节";
  217. }
  218. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"八月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"十五"]) {
  219. return @"中秋节";
  220. }
  221. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"九月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"初九"]) {
  222. return @"重阳节";
  223. }
  224. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"腊月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"初八"]) {
  225. return @"腊八节";
  226. }
  227. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"腊月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"廿三"]) {
  228. return @"北方小年";
  229. }
  230. if ([[lunarDic objectForKey:@"szNongliMonth"] isEqualToString:@"腊月"] && [[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"廿四"]) {
  231. return @"南方小年";
  232. }
  233. if ([[lunarDic objectForKey:@"szNongliDay"] isEqualToString:@"初一"]) {
  234. return [lunarDic objectForKey:@"szNongliMonth"];
  235. }
  236. return [lunarDic objectForKey:@"szNongliDay"];
  237. }
  238. -(NSDictionary *)LunarForSolarYear:(int)wCurYear Month:(int)wCurMonth Day:(int)wCurDay{
  239. //农历日期名
  240. NSArray *cDayName = [NSArray arrayWithObjects:@"*",
  241. @"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十",
  242. @"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十",
  243. @"廿一",@"廿二",@"廿三",@"廿四",@"廿五",@"廿六",@"廿七",@"廿八",@"廿九",@"三十",
  244. nil];
  245. //农历月份名
  246. NSArray *cMonName = [NSArray arrayWithObjects:@"*",@"正月",@"二月",@"三月",@"四月",@"五月",@"六月",@"七月",@"八月",@"九月",@"十月",@"十一月",@"腊月",nil];
  247. //公历每月前面的天数
  248. const int wMonthAdd[12] = {0,31,59,90,120,151,181,212,243,273,304,334};
  249. //农历数据
  250. const int wNongliData[100] = {2635,333387,1701,1748,267701,694,2391,133423,1175,396438
  251. ,3402,3749,331177,1453,694,201326,2350,465197,3221,3402
  252. ,400202,2901,1386,267611,605,2349,137515,2709,464533,1738
  253. ,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762
  254. ,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413
  255. ,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395
  256. ,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031
  257. ,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222
  258. ,268949,3402,3493,133973,1386,464219,605,2349,334123,2709
  259. ,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877};
  260. static int nTheDate,nIsEnd,m,k,n,i,nBit;
  261. //计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
  262. nTheDate = (wCurYear - 1921) * 365 + (wCurYear - 1921) / 4 + wCurDay + wMonthAdd[wCurMonth - 1] - 38;
  263. if((!(wCurYear % 4)) && (wCurMonth > 2))
  264. nTheDate = nTheDate + 1;
  265. //计算农历天干、地支、月、日
  266. nIsEnd = 0;
  267. m = 0;
  268. while(nIsEnd != 1)
  269. {
  270. if(wNongliData[m] < 4095)
  271. k = 11;
  272. else
  273. k = 12;
  274. n = k;
  275. while(n>=0)
  276. {
  277. //获取wNongliData(m)的第n个二进制位的值
  278. nBit = wNongliData[m];
  279. for(i=1;i < n+1;i++)
  280. nBit = nBit/2;
  281. nBit = nBit % 2;
  282. if (nTheDate <= (29 + nBit))
  283. {
  284. nIsEnd = 1;
  285. break;
  286. }
  287. nTheDate = nTheDate - 29 - nBit;
  288. n = n - 1;
  289. }
  290. if(nIsEnd)
  291. break;
  292. m = m + 1;
  293. }
  294. wCurYear = 1921 + m;
  295. wCurMonth = k - n + 1;
  296. wCurDay = nTheDate;
  297. if (k == 12)
  298. {
  299. if (wCurMonth == wNongliData[m] / 65536 + 1)
  300. wCurMonth = 1 - wCurMonth;
  301. else if (wCurMonth > wNongliData[m] / 65536 + 1)
  302. wCurMonth = wCurMonth - 1;
  303. }
  304. //生成农历月
  305. NSString *szNongliMonth;
  306. if (wCurMonth < 1){
  307. szNongliMonth = [NSString stringWithFormat:@"闰%@",(NSString *)[cMonName objectAtIndex:-1 * wCurMonth]];
  308. }else{
  309. szNongliMonth = (NSString *)[cMonName objectAtIndex:wCurMonth];
  310. }
  311. //生成农历日
  312. NSString *szNongliDay = [cDayName objectAtIndex:wCurDay];
  313. //合并
  314. // NSString *lunarDate = [NSString stringWithFormat:@"%@-%@",szNongliMonth,szNongliDay];
  315. // return lunarDate;
  316. NSMutableDictionary *lunarDateDic = [[NSMutableDictionary alloc]init];
  317. [lunarDateDic setValue:szNongliMonth forKey:@"szNongliMonth"];
  318. [lunarDateDic setValue:szNongliDay forKey:@"szNongliDay"];
  319. return lunarDateDic;
  320. }
  321. -(void)setTodayText:(BOOL )isHaveToday
  322. {
  323. // if (isHaveToday) {
  324. // self.dayLabel.text = @"今天";
  325. // self.dayLabel.textColor = [UIColor orangeColor];
  326. // self.actionLabel.textColor = [UIColor orangeColor];
  327. // if (self.model) {
  328. // if (self.model.state == DayModelStateStart) {
  329. // self.dayLabel.textColor = [UIColor whiteColor];
  330. // self.actionLabel.textColor = [UIColor whiteColor];
  331. // }else{
  332. // self.dayLabel.textColor = [UIColor orangeColor];
  333. // self.actionLabel.textColor = [UIColor orangeColor];
  334. // }
  335. // }
  336. // }else if(self.model){
  337. // [self fullCellWithModel:self.model];
  338. // }else{
  339. // self.dayLabel.text = @"";
  340. // }
  341. }
  342. - (void)awakeFromNib {
  343. [super awakeFromNib];
  344. self.colorView.layer.cornerRadius = 4;
  345. self.colorView.layer.masksToBounds = YES;
  346. self.actionLabel.adjustsFontSizeToFitWidth = YES;
  347. }
  348. @end