宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-11 4356615a9252a987a62469331b1fcf91c102e24c
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
//
//  EKNotificationMessageView.swift
//  SwiftEntryKit
//
//  Created by Daniel Huri on 4/19/18.
//  Copyright (c) 2018 huri000@gmail.com. All rights reserved.
//
 
import UIKit
 
final public class EKNotificationMessageView: EKSimpleMessageView {
    
    // MARK: Props
    private var auxLabel: UILabel!
    private var auxiliaryContent: EKProperty.LabelContent!
    
    private let message: EKNotificationMessage
    
    // MARK: Setup
    public init(with message: EKNotificationMessage) {
        self.message = message
        super.init(with: message.simpleMessage)
        setupAuxLabel(with: message.auxiliary)
        layoutContent(with: message.insets)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupAuxLabel(with content: EKProperty.LabelContent?) {
        auxiliaryContent = content
        guard let content = content else {
            return
        }
        auxLabel = UILabel()
        auxLabel.content = content
        addSubview(auxLabel)
    }
    
    private func layoutContent(with insets: EKNotificationMessage.Insets) {
        messageContentView.verticalMargins = 0
        messageContentView.horizontalMargins = 0
        messageContentView.labelsOffset = insets.titleToDescription
        
        if let thumbImageView = thumbImageView {
            thumbImageView.layoutToSuperview(.left, offset: insets.contentInsets.left)
            thumbImageView.layoutToSuperview(.top, offset: insets.contentInsets.top)
            messageContentView.layout(.left, to: .right, of: thumbImageView, offset: 12)
            messageContentView.layout(to: .top, of: thumbImageView, offset: 4)
        } else {
            messageContentView.layoutToSuperview(.left, offset: insets.contentInsets.left)
            messageContentView.layoutToSuperview(.top, offset: insets.contentInsets.top)
        }
 
        if let auxLabel = auxLabel {
            auxLabel.layoutToSuperview(.right, offset: -insets.contentInsets.right)
            auxLabel.layoutToSuperview(.top, offset: insets.contentInsets.top + 2)
            auxLabel.forceContentWrap()
            messageContentView.layout(.right, to: .left, of: auxLabel)
        } else {
            messageContentView.layoutToSuperview(.right, offset: -insets.contentInsets.right)
        }
        messageContentView.layoutToSuperview(.bottom, offset: -insets.contentInsets.bottom)
    }
    
    public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        auxLabel?.textColor = auxiliaryContent?.style.color(for: traitCollection)
    }
}