JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//
//  Copyright © 2018年 Zhejiang Dahua Technology Co.,Ltd. All rights reserved.
//
 
import UIKit
 
enum DHAddGuideActionType {
    case next
    case detail
    case check
    case error
}
 
protocol DHAddGuideViewDelegate: NSObjectProtocol {
    
    func guideView(view: DHAddGuideView, action: DHAddGuideActionType)
}
 
class DHAddGuideView: UIView {
    
    /// 是否选中
    public var isChecked: Bool {
        get {
            return checkButton.isSelected
        }
        set {
            checkButton.isSelected = newValue
        }
    }
    
    @IBOutlet weak var topImageView: UIImageView!
    @IBOutlet weak var topTipLabel: UITextView!
    @IBOutlet weak var descriptionLabel: UILabel!
    @IBOutlet weak var detailButton: UIButton!
    @IBOutlet weak var checkButton: UIButton!
    @IBOutlet weak var nextButton: UIButton!
    @IBOutlet weak var nextButtonBottomConstraint: NSLayoutConstraint!
    
    lazy var errorButton: UIButton = {
        let errorButton = UIButton()
//
        errorButton.setTitle("自动连接失败".lc_T, for: .normal)
        errorButton.setTitleColor(UIColor.dhcolor_c2(), for: .normal)
        errorButton.setImage(UIImage(named: "adddevice_icon_help"), for: .normal)
        errorButton.addTarget(self, action: #selector(errorButtonClicked), for: .touchUpInside)
        errorButton.isHidden = true
        
        return errorButton
    }()
    
    public weak var delegate: DHAddGuideViewDelegate?
    private var tapUnderlineAction: (() -> Void)? = nil
    private var rects: [CGRect] = []    // 存储下划线字段的点击rect
    
    //    deinit {
    //        dh_printDeinit(self)
    //    }
    
    public static func xibInstance() -> DHAddGuideView {
        if let view = Bundle.dh_addDeviceBundle()?.loadNibNamed("DHAddGuideView", owner: nil, options: nil)!.first as? DHAddGuideView {
            return view
        }
        return DHAddGuideView()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = UIColor.dhcolor_c43()
        
        self.topImageView.contentMode = .scaleAspectFit
        self.topImageView.image = UIImage(named: "adddevice_netsetting_power")
        
        //清除默认显示的文字
        self.topTipLabel.textAlignment = .center
        self.topTipLabel.text = nil
        self.topTipLabel.isSelectable = false
        self.topTipLabel.isEditable = false
        self.topTipLabel.delegate = self
        self.topTipLabel.tintColor = UIColor.dhcolor_c0()
        topTipLabel.isUserInteractionEnabled = true
        self.descriptionLabel.textAlignment = .center
        self.descriptionLabel.text = nil
        self.detailButton.setTitle(nil, for: .normal)
        self.detailButton.setAttributedTitle(nil, for: .normal)
        self.nextButton.setTitle("common_next".lc_T, for: .normal)
        
        //多行显示
        self.checkButton.titleLabel?.numberOfLines = 2
        self.detailButton.titleLabel?.numberOfLines = 2
        
        //配置颜色、样式
        self.topTipLabel.textColor = UIColor.dhcolor_c2()
        self.descriptionLabel.textColor = UIColor.dhcolor_c5()
        self.detailButton.setTitleColor(UIColor.dhcolor_c0(), for: .normal)
        self.checkButton.setTitleColor(UIColor.dhcolor_c5(), for: .normal)
        self.nextButton.setTitleColor(UIColor.dhcolor_c43(), for: .normal)
        
        self.nextButton.layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
        self.nextButton.backgroundColor = DHModuleConfig.shareInstance().commonButtonColor()
        self.checkButton.setImage(UIImage(named: "adddevice_box_checkbox"), for: .normal)
        self.checkButton.setImage(UIImage(named: "adddevice_box_checkbox_checked"), for: .selected)
        
        //默认不显示check按钮、描述文字、详情按钮、重置视图
        self.setCheckHidden(hidden: true)
        self.descriptionLabel.text = nil
        self.detailButton.setTitle("", for: .normal)
        self.setDetailButtonHidden(hidden: true)
        addSubview(errorButton)
        
        //配置默认约束
        self.setupConstraints()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        errorButton.setUIButtonImageRightWithTitleLeftUI()
    }
    
    private func setupConstraints() {
        topImageView.snp.makeConstraints { make in
            make.centerX.equalTo(self)
            make.top.equalTo(40)
            make.width.equalTo(240)
            make.height.lessThanOrEqualTo(240)
        }
        
        errorButton.snp.makeConstraints { (make) in
            make.top.equalTo(topImageView.snp.bottom).offset(15)
            make.centerX.equalTo(self)
        }
        
        topTipLabel.snp.makeConstraints { make in
            make.leading.equalTo(self).offset(15)
            make.top.equalTo(errorButton.snp.bottom)
            make.centerX.equalTo(self)
            make.bottom.greaterThanOrEqualTo(descriptionLabel.snp.top).offset(-5)
        }
        
        descriptionLabel.snp.makeConstraints { make in
            make.leading.equalTo(self).offset(13)
            make.centerX.equalTo(self)
            make.bottom.greaterThanOrEqualTo(detailButton.snp.top).offset(-5)
        }
        
        detailButton.snp.makeConstraints { make in
            make.leading.equalTo(self).offset(15)
            make.top.equalTo(descriptionLabel.snp.bottom).offset(5)
            make.bottom.lessThanOrEqualTo(self).offset(-5)
            make.centerX.equalTo(self)
        }
        
        if dh_isiPhoneX {
            nextButtonBottomConstraint.constant += dh_bottomSafeMargin
        }
    }
    
    // MARK: Actions
    @IBAction func onCheckAction(_ sender: Any) {
        checkButton.isSelected = !checkButton.isSelected
        nextButton.dh_enable = checkButton.isSelected
        delegate?.guideView(view: self, action: .check)
    }
    
    @IBAction func onNextAction(_ sender: Any) {
        delegate?.guideView(view: self, action: .next)
    }
    
    @IBAction func onDetailAction(_ sender: Any) {
        delegate?.guideView(view: self, action: .detail)
    }
    
    @objc private func onTapDescriptionLabelAction(tapGesture: UIGestureRecognizer) {
        
        if let textView = tapGesture.view as? UITextView {
            if #available(iOS 10.0, *) {
                let tapLocation = tapGesture.location(in: textView)
                let textPosition = textView.closestPosition(to: tapLocation)
                guard textPosition != nil else {
                    return
                }
                
                let attr = textView.textStyling(at: textPosition!, in: .forward)
                if let url = attr?[NSAttributedStringKey.link.rawValue] as? String {
                    print("url: \(url)")
                    if url == "copy://" {
                        self.tapUnderlineAction?()
                    }
                }
            }else {
                self.tapUnderlineAction?()
            }
            
        }
    }
    
    @objc func errorButtonClicked() {
        delegate?.guideView(view: self, action: .error)
    }
    
    // MARK: Configurations
    /// 设置确认按钮隐藏,隐藏时nextButton直接可以点击
    ///
    /// - Parameter hidden: true/false
    public func setCheckHidden(hidden: Bool) {
        checkButton.isHidden = hidden
        nextButton.dh_enable = hidden
    }
    
    public func setDetailButtonHidden(hidden: Bool) {
        detailButton.isHidden = hidden
    }
    
    public func setCheck(checked: Bool) {
        checkButton.isSelected = checked
        nextButton.dh_enable = checked
    }
    
    /// 设置详情文字,如果文字内容为空,隐藏按钮,防止触发点击事件
    ///
    /// - Parameter text: 文字
    /// - useUnderline: 是否使用下划线
    /// - tap: 点击事件的回调
    public func setTopTipLabel(text: String, underlineString: String? = nil, shouldCopy: Bool = false, tap: (() -> Void)? = nil) {
        
        //IOS11 这个地方不能这么写errorButton显示隐藏应该由外部控制  这里先暂时
        if #available(iOS 11.0, *) { } else {
            
            errorButton.isHidden = true
        
        }
        
        topTipLabel.isHidden = false
        
        // Common
        let paragraph = NSMutableParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        paragraph?.alignment = .center
        paragraph?.lineSpacing = 10
        
        let attributedString: NSMutableAttributedString =
            NSMutableAttributedString(string: text, attributes: [
                NSAttributedString.Key.paragraphStyle: paragraph ?? NSParagraphStyle.default,
                NSAttributedString.Key.font: UIFont.dhFont_t3()
            ])
        
        // 字符串下划线
        if let _underlineString = underlineString {
            let userPolicyStr: NSMutableAttributedString = NSMutableAttributedString(string: _underlineString)
            let userPolicyStrRange: NSRange = NSRange(location: 0, length: _underlineString.length)
            userPolicyStr.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.dhcolor_c0()], range: userPolicyStrRange)
            if shouldCopy {
                userPolicyStr.addAttributes([NSAttributedStringKey.link: "copy://"], range: userPolicyStrRange)
                userPolicyStr.addAttributes([NSAttributedString.Key.underlineStyle: 1], range: userPolicyStrRange)
            }
            
            userPolicyStr.addAttributes([NSAttributedString.Key.font: UIFont.dhFont_t3()], range: userPolicyStrRange)
            attributedString.append(userPolicyStr)
            
            if shouldCopy {
                let imageAttachment = NSTextAttachment()
                imageAttachment.image = UIImage.init(named: "setting_icon_copy.png")?.withRenderingMode(.alwaysOriginal)
                imageAttachment.bounds = CGRect(x: 0, y: -4, width: 21, height: 21)
                let imageString = NSAttributedString(attachment: imageAttachment)
                attributedString.append(imageString)
            }
            
            topTipLabel.attributedText = attributedString
            
            
            
            topTipLabel.delegate = self
            
        } else {
            topTipLabel.attributedText = attributedString
        }
        
        // 点击事件
        if let closure = tap, shouldCopy {
            self.tapUnderlineAction = closure
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTapDescriptionLabelAction(tapGesture:)))
            tapGesture.delegate = self
            topTipLabel.isUserInteractionEnabled = true
            topTipLabel.addGestureRecognizer(tapGesture)
        }
    }
 
    
    /// 设置详情按钮的文字,如果文字内容为空,隐藏按钮,防止触发点击事件
    ///
    /// - Parameter text: 文字
    /// - useUnderline: 是否使用下划线
    public func setDetailButton(text: String?, useUnderline: Bool = false) {
        if text == nil || text!.count == 0 {
            detailButton.isHidden = true
            return
        }
        
        detailButton.isHidden = false
        
        if useUnderline {
            let attrString = NSMutableAttributedString(string: text!)
            let range = NSMakeRange(0, text!.count)
            let number = NSNumber(integerLiteral: NSUnderlineStyle.styleSingle.rawValue)
            
            attrString.addAttributes([NSAttributedStringKey.underlineStyle: number,
                                      NSAttributedStringKey.foregroundColor: UIColor.dhcolor_c0()], range: range)
            detailButton.setAttributedTitle(attrString, for: .normal)
        } else {
            detailButton.setTitle(text, for: .normal)
        }
    }
    
    // MARK: Update constraint
    public func updateTopImageViewConstraint(top: CGFloat, width: CGFloat, maxHeight: CGFloat? = 0) {
        topImageView.snp.updateConstraints { make in
            make.top.equalTo(top)
            make.width.equalTo(width)
            if maxHeight != nil {
                make.height.lessThanOrEqualTo(maxHeight!)
            }
        }
    }
    
    public func updateContentLabelConstraint(top: CGFloat) {
        errorButton.snp.updateConstraints { (make) in
            make.top.equalTo(topImageView.snp.bottom).offset(top)
            make.centerX.equalTo(self)
        }
        
        topTipLabel.snp.updateConstraints { (make) in
            make.top.equalTo(errorButton.snp.bottom)
        }
    }
    
    public func updateDetailButtonlConstraint(bottom: CGFloat) {
        detailButton.snp.remakeConstraints { make in
            make.leading.equalTo(self).offset(13)
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset( dh_isiPhoneX ? bottom - dh_bottomSafeMargin : bottom )
        }
        
        errorButton.snp.remakeConstraints { (make) in
            make.top.equalTo(topImageView.snp.bottom).offset(15)
            make.centerX.equalTo(self)
        }
        
        topTipLabel.snp.remakeConstraints { make in
            make.leading.equalTo(self).offset(15)
            make.top.equalTo(errorButton.snp.bottom)
            make.centerX.equalTo(self)
            make.bottom.greaterThanOrEqualTo(descriptionLabel.snp.top).offset(-5)
        }
        
        descriptionLabel.snp.remakeConstraints { make in
            make.leading.equalTo(self).offset(13)
            make.top.equalTo(topTipLabel.snp.bottom).offset(10)
            make.centerX.equalTo(self)
            //            make.bottom.greaterThanOrEqualTo(detailButton.snp.top).offset(-5)
        }
    }
}
 
 
extension DHAddGuideView: UIGestureRecognizerDelegate, UITextViewDelegate {
    
//    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
//
//        guard let textView = gestureRecognizer.view as? UITextView else { return false }
//
//        let point = gestureRecognizer.location(in: textView)
//
//        for rect in self.rects {
//            if rect.contains(point) {
//                return true
//            }
//            return false
//        }
//
//        return false
//    }
    
    private func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
        
        print(URL)
        return true
    }
 
}