JLChen
2021-05-17 a722e767f98042d5ef6259d2dde7854c925e4167
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
//  Copyright © 2019 jm. All rights reserved.
//
 
import UIKit
import SnapKit
 
open class LCAlertView: UIView {
    
    // MARK: life cycle
    
    convenience init(title: String, message: String, cancelTitle: String? = nil, confirmTitle: String? = nil, moreView: UIView? = nil, cancelHandler: (() -> ())? = nil, confirmHandler: (() -> ())? = nil) {
        self.init(frame: CGRect.zero)
        
        self.titleLabel.text = title
        self.messageLabel.text = message
        self.moreView = moreView
        self.cancelTitle = cancelTitle
        self.confirmTitle = confirmTitle
        self.cancelButton.setTitle(cancelTitle, for: .normal)
        self.confirmButton.setTitle(confirmTitle, for: .normal)
        self.cancelHandler = cancelHandler
        self.confirmHandler = confirmHandler
        
        loadSubviews()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    override open func sizeThatFits(_ size: CGSize) -> CGSize {
        let statusLabelText: NSString = NSString(string: self.messageLabel.text ?? "")
        let attrButes = [NSAttributedStringKey.font: UIFont.dhFont_t5()]
        let size: CGRect = statusLabelText.boundingRect(with: CGSize(width: 275, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attrButes as [NSAttributedStringKey: Any], context: nil)
    
        var height = size.height + 155
        if let `moreView` = moreView {
            height += (moreView.dh_height + 13)
        }
        
        return CGSize(width: 305, height: height)
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - private method
    
    private func loadSubviews() {
        backgroundColor = UIColor.dhcolor_c43()
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self).offset(padding15)
            make.centerX.equalTo(self)
        }
        
        addSubview(messageLabel)
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(padding20)
            make.left.equalTo(self).offset(padding15)
            make.centerX.equalTo(self)
        }
        
        if let `moreView` = moreView {
            addSubview(moreView)
            moreView.snp.makeConstraints({ (make) in
                make.top.equalTo(messageLabel.snp.bottom).offset(24)
                make.centerX.equalTo(self)
                make.size.equalTo(moreView.dh_size)
            })
        }
 
        addSubview(horizontalLine)
        horizontalLine.snp.makeConstraints { (make) in
            make.left.right.equalTo(self)
            make.height.equalTo(1 / UIScreen.main.scale)
            make.top.equalTo(messageLabel.snp.bottom).offset(moreView == nil ? padding35 : (48 + (moreView?.dh_height ?? 0)))
        }
 
        if cancelTitle == nil || confirmTitle == nil {
 
            if cancelTitle == nil {
                addSubview(confirmButton)
                confirmButton.snp.makeConstraints { (make) in
                    make.top.equalTo(horizontalLine.snp.bottom)
                    make.left.right.bottom.equalTo(self)
                    make.height.equalTo(buttonHeight)
                }
            } else if confirmTitle == nil {
                addSubview(cancelButton)
                cancelButton.snp.makeConstraints { (make) in
                    make.top.equalTo(horizontalLine.snp.bottom)
                    make.left.right.bottom.equalTo(self)
                    make.height.equalTo(buttonHeight)
                }
            }
 
        } else {
            addSubview(verticalLine)
            verticalLine.snp.makeConstraints { (make) in
                make.top.equalTo(horizontalLine.snp.bottom)
                make.left.equalTo(self).offset(sizeThatFits(CGSize.zero).width / 2)
                make.width.equalTo(1 / UIScreen.main.scale)
                make.height.equalTo(buttonHeight)
                make.bottom.equalToSuperview()
            }
            
            addSubview(cancelButton)
            cancelButton.snp.makeConstraints { (make) in
                make.top.equalTo(horizontalLine.snp.bottom)
                make.left.equalTo(self)
                make.right.equalTo(verticalLine.snp.left)
                make.height.equalTo(verticalLine.snp.height)
            }
 
            addSubview(confirmButton)
            confirmButton.snp.makeConstraints { (make) in
                make.top.equalTo(horizontalLine.snp.bottom)
                make.right.equalTo(self)
                make.height.equalTo(verticalLine.snp.height)
                make.left.equalTo(verticalLine.snp.right)
            }
        }
    }
    
    @objc func cancelButtonClicked() {
        if isCanClicked {
            cancelHandler?()
            isCanClicked = false
            resetButton()
        }
    }
    
    @objc func confirmButtonClicked() {
        if isCanClicked {
            confirmHandler?()
            isCanClicked = false
            resetButton()
        }
    }
    
    private func resetButton() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
            self?.isCanClicked = true
        }
    }
    
    // MARK: - lazy ui
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = UIFont.dhFont_t2()
        titleLabel.textColor = UIColor.dhcolor_c2()
        
        return titleLabel
    }()
    
    lazy var messageLabel: UILabel = {
        let messageLabel = UILabel()
        messageLabel.font = UIFont.dhFont_t5()
        messageLabel.textColor = UIColor.dhcolor_c2()
        messageLabel.numberOfLines = 0
        messageLabel.textAlignment = .center
        messageLabel.lineBreakMode = .byWordWrapping
        
        return messageLabel
    }()
    
    lazy var cancelButton: UIButton = {
        let cancelButton = UIButton()
        cancelButton.titleLabel?.font = UIFont.dhFont_t2()
        cancelButton.setTitleColor(UIColor.dhcolor_c2(), for: .normal)
        cancelButton.addTarget(self, action: #selector(cancelButtonClicked), for: .touchUpInside)
        
        return cancelButton
    }()
    
    lazy var horizontalLine: UIView = {
        let horizontalLine = UIView()
        horizontalLine.backgroundColor = UIColor.dhcolor_c8()
        
        return horizontalLine
    }()
    
    lazy var verticalLine: UIView = {
        let verticalLine = UIView()
        verticalLine.backgroundColor = UIColor.dhcolor_c8()
        
        return verticalLine
    }()
    
    lazy var confirmButton: UIButton = {
        let confirmButton = UIButton()
        confirmButton.titleLabel?.font = UIFont.dhFont_t2()
        confirmButton.setTitleColor(UIColor.dhcolor_c0(), for: .normal)
        confirmButton.addTarget(self, action: #selector(confirmButtonClicked), for: .touchUpInside)
        
        return confirmButton
    }()
    
    // MARK: - private var
    
    private var moreView: UIView? = nil
    private var cancelHandler: (() -> ())? = nil
    private var confirmHandler: (() -> ())? = nil
    private var cancelTitle: String? = nil {
        didSet {
            guard let `cancelTitle` = cancelTitle else {
                return
            }
            
            if oldValue != cancelTitle {
                self.cancelButton.setTitle(cancelTitle, for: .normal)
            }
        }
    }
    
    private var confirmTitle: String? = nil {
        didSet {
            guard let `confirmTitle` = confirmTitle else {
                return
            }
            
            if oldValue != confirmTitle {
                self.confirmButton.setTitle(confirmTitle, for: .normal)
            }
        }
    }
    
    private var isCanClicked: Bool = true
    private var horizontalLineBottomConstraint: Constraint?
    private let buttonHeight: CGFloat = 50.0
    private let padding35: CGFloat = 35.0
    private let padding20: CGFloat = 20.0
    private let padding15: CGFloat = 15.0
}