jgzhou
2021-04-09 6b6c0cebff891b797c3cb6b99218b63b04d4c922
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
//  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