-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathAutoSizingContainerView.swift
68 lines (55 loc) · 1.83 KB
/
AutoSizingContainerView.swift
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
import UIKit
final class AutoSizingContainerView: UIView {
private let imageView = UIImageView()
private let firstTextLabel = UILabel()
private let secondTextLabel = UILabel()
private let margin: CGFloat = 10
@Proxy(\AutoSizingContainerView.imageView.image)
var image: UIImage? {
didSet {
setNeedsLayout()
}
}
@Proxy(\AutoSizingContainerView.firstTextLabel.text)
var firstText: String? {
didSet {
setNeedsLayout()
}
}
@Proxy(\AutoSizingContainerView.secondTextLabel.text)
var secondText: String? {
didSet {
setNeedsLayout()
}
}
init() {
super.init(frame: CGRect.zero)
configureView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func configureView() {
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
addSubview(imageView)
firstTextLabel.numberOfLines = 0
firstTextLabel.backgroundColor = UIColor.orange.withAlphaComponent(0.3)
addSubview(firstTextLabel)
secondTextLabel.numberOfLines = 0
secondTextLabel.backgroundColor = UIColor.green.withAlphaComponent(0.3)
addSubview(secondTextLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
performLayout()
}
private func performLayout() {
imageView.pin.top().horizontally().sizeToFit(.width).margin(margin)
firstTextLabel.pin.below(of: imageView).horizontally().sizeToFit(.width).margin(margin)
secondTextLabel.pin.below(of: firstTextLabel).horizontally().sizeToFit(.width).margin(margin)
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
autoSizeThatFits(size, layoutClosure: performLayout)
}
}