// // ViewController.m // Lunar // // Created by zhoujigang on 2021/4/1. // #import "ViewController.h" #import "Lunar.h" #define kWidth [UIScreen mainScreen].bounds.size.width #define kHeight [UIScreen mainScreen].bounds.size.height #define kStatusBarHeight [UIApplication sharedApplication].statusBarFrame.size.height @interface ViewController () @property (nonatomic, strong) NSDateFormatter *dateFormatter; @property (nonatomic, strong) UIButton *dateBtn; @property (nonatomic, strong) UITextView *textView; @property (nonatomic, strong) UIDatePicker *picker; @property (nonatomic, strong) UIButton *cancelBtn; @property (nonatomic, strong) UIButton *confirmBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.dateBtn]; [self.view addSubview:self.textView]; [self buildUI:[NSDate date]]; } - (void)dateClick:(UIButton *)sender { [self.view addSubview:self.picker]; [self.dateBtn addSubview:self.cancelBtn]; [self.dateBtn addSubview:self.confirmBtn]; } - (void)actionBtnClick:(UIButton *)sender { [self.picker removeFromSuperview]; [self.cancelBtn removeFromSuperview]; [self.confirmBtn removeFromSuperview]; if (sender.tag == 2) { [self buildUI:self.picker.date]; } } - (void)buildUI:(NSDate *)date { NSString *dateStr = [self.dateFormatter stringFromDate:date]; [self.dateBtn setTitle:dateStr forState:UIControlStateNormal]; NSString *dataStr = [self buildData:date]; self.textView.text = dataStr; } - (NSString *)buildData:(NSDate *)date { NSMutableString *str = [NSMutableString string]; Solar *d = [Solar fromDate:date]; Lunar *l = [d getLunar]; [str appendFormat:@"农历:%@\n", [l toString]]; [str appendFormat:@"\n年:%@年 属%@ %@\n", [l getYearInGanZhi], [l getYearShengXiao], [l getYearNaYin]]; [str appendFormat:@"\n月:%@月 属%@ %@\n", [l getMonthInGanZhi], [l getMonthShengXiao], [l getMonthNaYin]]; [str appendFormat:@"\n日:%@日 属%@ %@\n", [l getDayInGanZhi], [l getDayShengXiao], [l getDayNaYin]]; [str appendFormat:@"\n儒略日:%@\n", @([d getJulianDay])]; [str appendFormat:@"\n月名:%@\n", [l getSeason]]; [str appendFormat:@"\n月相:%@\n", [l getYueXiang]]; [str appendFormat:@"\n物候:%@\n", [l getWuHou]]; [str appendFormat:@"\n六曜:%@\n", [l getLiuYao]]; [str appendFormat:@"\n彭祖百忌:%@ %@\n", [l getPengZuGan], [l getPengZuZhi]]; NSArray *arrDayYi = [l getDayYi]; [str appendFormat:@"\n每日宜:%@\n", arrDayYi.count == 0?@"无":[arrDayYi componentsJoinedByString:@" "]]; NSArray *arrDayJi = [l getDayJi]; [str appendFormat:@"\n每日忌:%@\n", arrDayJi.count == 0?@"无":[arrDayJi componentsJoinedByString:@" "]]; NSArray *arrDayJiShen = [l getDayJiShen]; [str appendFormat:@"\n吉神宜趋:%@\n", arrDayJiShen.count == 0?@"无":[arrDayJiShen componentsJoinedByString:@" "]]; NSArray *arrDayXiongSha = [l getDayXiongSha]; [str appendFormat:@"\n凶煞宜忌:%@\n", arrDayXiongSha.count == 0?@"无":[arrDayXiongSha componentsJoinedByString:@" "]]; [str appendFormat:@"\n相冲:%@日 冲%@\n", [l getDayShengXiao], [l getDayChongDesc]]; [str appendFormat:@"\n岁煞:%@\n", [l getDaySha]]; [str appendFormat:@"\n星宿:%@(%@)\n", [l getXiu], [l getXiuLuck]]; [str appendFormat:@"\n星宿歌诀:%@\n", [l getXiuSong]]; [str appendFormat:@"\n贵神方位:阳贵神:%@ 阴贵神:%@\n", [l getDayPositionYangGuiDesc], [l getDayPositionYinGuiDesc]]; [str appendFormat:@"\n喜神方位:%@\n", [l getDayPositionXiDesc]]; [str appendFormat:@"\n福神方位:%@\n", [l getDayPositionFuDesc]]; [str appendFormat:@"\n财神方位:%@\n", [l getDayPositionCaiDesc]]; [str appendFormat:@"\n本月胎神:%@\n", [l getMonthPositionTai]]; [str appendFormat:@"\n今日胎神:%@\n", [l getDayPositionTai]]; [str appendFormat:@"\n值星:%@\n", [l getZhiXing]]; [str appendFormat:@"\n十二天神:%@(%@)%@\n", [l getDayTianShen], [l getDayTianShenType], [l getDayTianShenLuck]]; [str appendFormat:@"\n空亡所值:年=%@ 月=%@ 日=%@\n", [l getYearXunKong], [l getMonthXunKong], [l getDayXunKong]]; NineStar *dayNineStar = [l getDayNineStar]; [dayNineStar toFullString]; [str appendFormat:@"\n九星:%@%@-%@星(%@)-%@\n", [dayNineStar getNumber],[dayNineStar getColor], [dayNineStar getNameInTaiYi], [dayNineStar getWuXing], [dayNineStar getTypeInTaiYi]]; [str appendFormat:@"\n九星歌诀:%@\n", [dayNineStar getSongInTaiYi]]; JieQi * pJQ = [l getPrevJieQi]; [str appendFormat:@"\n上一节气:%@ %@\n", pJQ.name, [pJQ.solar toString]]; JieQi * nJQ = [l getNextJieQi]; [str appendFormat:@"\n下一节气:%@ %@\n", nJQ.name, [nJQ.solar toString]]; NSArray *arrFestivals = [d getFestivals]; [str appendFormat:@"\n节日:%@\n", arrFestivals.count == 0?@"无":[arrFestivals componentsJoinedByString:@" "]]; NSArray *arrOtherFestivals = [d getOtherFestivals]; [str appendFormat:@"\n纪念日:%@\n", arrOtherFestivals.count == 0?@"无":[arrOtherFestivals componentsJoinedByString:@" "]]; ShuJiu *shujiu = [l getShuJiu]; [str appendFormat:@"\n数九:%@\n", shujiu?[shujiu toFullString]:@"无"]; Fu *fu = [l getFu]; [str appendFormat:@"\n三伏:%@\n", fu?[fu toFullString]:@"无"]; return str; } #pragma mark - lazy - - (NSDateFormatter *)dateFormatter { if (!_dateFormatter) { _dateFormatter = [NSDateFormatter new]; _dateFormatter.dateFormat = @"yyyy-MM-dd"; } return _dateFormatter; } - (UIButton *)dateBtn { if (!_dateBtn) { _dateBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_dateBtn setBackgroundColor:[UIColor lightGrayColor]]; _dateBtn.frame = CGRectMake(10, kStatusBarHeight + 10, kWidth-20, 40); [_dateBtn addTarget:self action:@selector(dateClick:) forControlEvents:UIControlEventTouchUpInside]; } return _dateBtn; } - (UITextView *)textView { if (!_textView) { _textView = [[UITextView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_dateBtn.frame)+10, kWidth-20, kHeight-(CGRectGetMaxY(_dateBtn.frame)+20))]; _textView.editable = NO; } return _textView; } - (UIDatePicker *)picker { if (!_picker) { _picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.dateBtn.frame)+10, kWidth-20, 150)]; _picker.datePickerMode = UIDatePickerModeDate; _picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; _picker.minimumDate = [self.dateFormatter dateFromString:@"1901-01-01"]; _picker.maximumDate = [self.dateFormatter dateFromString:@"2099-12-31"]; _picker.backgroundColor = [UIColor lightGrayColor]; } return _picker; } - (UIButton *)cancelBtn { if (!_cancelBtn) { _cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; _cancelBtn.frame = CGRectMake(0, 0, 50, CGRectGetHeight(self.dateBtn.frame)); [_cancelBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _cancelBtn.tag = 1; } return _cancelBtn; } - (UIButton *)confirmBtn { if (!_confirmBtn) { _confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal]; _confirmBtn.frame = CGRectMake(CGRectGetWidth(self.dateBtn.frame)-50, 0, 50, CGRectGetHeight(self.dateBtn.frame)); [_confirmBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside]; _confirmBtn.tag = 2; } return _confirmBtn; } @end