JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
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
//
//  Copyright © 2019 dahua. All rights reserved.
//
 
#import "LCOCAlertView.h"
#import "AppDelegate.h"
#import <LCBaseModule/UIColor+LeChange.h>
 
@interface LCOCAlertView ()
 
/// 回调代码块
@property (copy, nonatomic) LCAlertViewResultBlock block;
 
/// title
@property (strong, nonatomic) NSString *title;
 
/// detail
@property (strong, nonatomic) NSString *detail;
 
/// confirm
@property (strong, nonatomic) NSString *confirmTitle;
 
/// cancle
@property (strong, nonatomic) NSString *cancleTitle;
 
@end
 
@implementation LCOCAlertView
 
+ (void)lc_ShowAlertWith:(NSString *)title Detail:(NSString *)detail ConfirmTitle:(NSString *)confirmTitle CancleTitle:(NSString *)cancleTitle Handle:(LCAlertViewResultBlock)block {
    [[LCOCAlertView new] showAlertWith:title Detail:detail ConfirmTitle:confirmTitle CancleTitle:cancleTitle Complete:block];
}
 
+ (void)lc_ShowAlertWithContent:(NSString *)content {
    [[LCOCAlertView new] showAlertWith:nil Detail:content ConfirmTitle:@"确定" CancleTitle:nil Complete:nil];
}
 
- (void)showAlertWith:(NSString *)title Detail:(NSString *)detail ConfirmTitle:(NSString *)confirmTitle CancleTitle:(NSString *)cancleTitle Complete:(LCAlertViewResultBlock)block {
    self.block = block;
    self.title = title;
    self.detail = detail;
    self.confirmTitle = confirmTitle;
    self.cancleTitle = cancleTitle;
    ///如果都没有输入则默认显示确定
    if (!self.confirmTitle && !self.cancleTitle && [@"" isEqualToString:self.confirmTitle] && [@"" isEqualToString:self.cancleTitle]) {
        self.confirmTitle = @"确定";
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self setupView];
    });
}
 
- (void)setupView {
    weakSelf(self);
    UIView *backgroundView = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].keyWindow.frame];
    backgroundView.backgroundColor = [UIColor dhcolor_c51];
    AppDelegate *delageat = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delageat.window addSubview:backgroundView];
    self.backgroundColor = [UIColor dhcolor_c43];
    [backgroundView addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(backgroundView.mas_width).multipliedBy(0.75);
        make.centerY.mas_equalTo(backgroundView.mas_centerY);
        make.centerX.mas_equalTo(backgroundView.mas_centerX);
    }];
 
    UILabel *titleLab = [UILabel new];
    [self addSubview:titleLab];
    titleLab.text = self.title;
    titleLab.textColor = [UIColor dhcolor_c40];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
 
    UILabel *detailLab = [UILabel new];
    [self addSubview:detailLab];
    detailLab.text = self.detail;
    detailLab.textColor = [UIColor dhcolor_c41];
    detailLab.numberOfLines = 0;
    detailLab.lineBreakMode = NSLineBreakByWordWrapping;
    detailLab.textAlignment = NSTextAlignmentCenter;
    [detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLab.mas_bottom).offset(20);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
 
    LCButton *cancleBtn = [LCButton lcButtonWithType:LCButtonTypeCustom];
    [cancleBtn setTitle:self.cancleTitle forState:UIControlStateNormal];
    [cancleBtn setBorderWithStyle:LC_BORDER_DRAW_TOP | LC_BORDER_DRAW_RIGHT borderColor:[UIColor dhcolor_c59] borderWidth:1];
    [cancleBtn setTitleColor:[UIColor dhcolor_c40] forState:UIControlStateNormal];
    [self addSubview:cancleBtn];
    cancleBtn.touchUpInsideblock = ^(LCButton *_Nonnull btn) {
        [backgroundView removeFromSuperview];
        if (weakself.block) {
            weakself.block(NO);
        }
    };
 
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(detailLab.mas_bottom).offset(20);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
 
    LCButton *confirmBtn = [LCButton lcButtonWithType:LCButtonTypeCustom];
    [confirmBtn setTitle:self.confirmTitle forState:UIControlStateNormal];
    [confirmBtn setBorderWithStyle:LC_BORDER_DRAW_TOP borderColor:[UIColor dhcolor_c59] borderWidth:1];
    [confirmBtn setTitleColor:[UIColor dhcolor_c10] forState:UIControlStateNormal];
    [self addSubview:confirmBtn];
    confirmBtn.touchUpInsideblock = ^(LCButton *_Nonnull btn) {
        [backgroundView removeFromSuperview];
        if (weakself.block) {
            weakself.block(YES);
        }
    };
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(detailLab.mas_bottom).offset(20);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
 
    if (self.cancleTitle && self.confirmTitle) {
        NSArray *btnAry = @[cancleBtn, confirmBtn];
        [btnAry mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
        [btnAry mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(50);
            make.bottom.mas_equalTo(self.mas_bottom);
            make.top.mas_equalTo(detailLab.mas_bottom).offset(20);
        }];
    }
    if (!self.confirmTitle) {
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left);
            make.width.mas_equalTo(self.mas_width).multipliedBy(1);
        }];
        confirmBtn.hidden = YES;
    }
    if (!self.cancleTitle) {
        [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left);
            make.width.mas_equalTo(self.mas_width).multipliedBy(1);
        }];
        cancleBtn.hidden = YES;
    }
}
 
+ (void)lc_showTextFieldAlertTextFieldWithTitle:(NSString *)title Detail:(NSString *)detail Placeholder:(NSString *)placeholder
                                   ConfirmTitle:(NSString *)confirmTitle CancleTitle:(NSString *)cancleTitle Handle:(void (^)(BOOL isConfirmSelected, NSString *inputContent))block {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:detail preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        if (block) {
            block(YES, alertController.textFields.firstObject.text);
        }
    }];
 
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:cancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
        if (block) {
            block(NO, nil);
        }
    }];
    [alertController addAction:confirmAction];
    [alertController addAction:cancleAction];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
        textField.placeholder = placeholder;
    }];
    [[self topPresentOrRootController] presentViewController:alertController animated:YES completion:nil];
}
 
+ (UIViewController *)topPresentOrRootController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController)
        resultVC = [self _topViewController:resultVC.presentedViewController];
    return resultVC;
}
 
+ (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
@end