-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathProductButton.swift
105 lines (89 loc) · 4.09 KB
/
ProductButton.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
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
//
// ProductButton.swift
// Lockdown
//
// Created by Denis Aleshyn on 10/05/2024.
// Copyright © 2024 Confirmed Inc. All rights reserved.
//
import Foundation
import UIKit
final class ProductButton: UIButton {
lazy var iconImageView: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleAspectFit
image.image = UIImage(named: "grey-ellipse-1")
image.layer.masksToBounds = true
image.widthAnchor.constraint(equalToConstant: 16).isActive = true
image.heightAnchor.constraint(equalToConstant: 16).isActive = true
return image
}()
lazy var containerStack: UIStackView = {
let stack = UIStackView(frame: .zero)
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .horizontal
stack.alignment = .center
stack.distribution = .fillProportionally
stack.spacing = 0
return stack
}()
init(title: String, subtitle: String, toHighlight: String?, isSelected: Bool = false) {
super.init(frame: .zero)
self.tintColor = .white
self.backgroundColor = .clear
self.layer.cornerRadius = 8
self.layer.borderWidth = isSelected ? 3 : 1
self.layer.borderColor = isSelected ? UIColor.white.cgColor : UIColor.gray.cgColor
self.iconImageView.image = isSelected ? UIImage(named: "fill-2") : UIImage(named: "grey-ellipse-1")
let planPeriodLabel = UILabel()
planPeriodLabel.translatesAutoresizingMaskIntoConstraints = false
planPeriodLabel.font = fontSemiBold15
planPeriodLabel.textColor = .white
planPeriodLabel.textAlignment = .left
planPeriodLabel.text = title
let planPriceLabel = UILabel()
planPriceLabel.translatesAutoresizingMaskIntoConstraints = false
planPriceLabel.font = fontRegular15
planPriceLabel.textColor = .white
planPriceLabel.textAlignment = .left
planPriceLabel.text = subtitle
if let toHighlight {
planPriceLabel.highlight(toHighlight, font: UIFont.boldLockdownFont(size: 16))
}
let subscriptionPlanStack = UIStackView()
subscriptionPlanStack.addArrangedSubview(planPeriodLabel)
subscriptionPlanStack.addArrangedSubview(planPriceLabel)
subscriptionPlanStack.translatesAutoresizingMaskIntoConstraints = false
subscriptionPlanStack.axis = .vertical
subscriptionPlanStack.alignment = .leading
subscriptionPlanStack.distribution = .fill
subscriptionPlanStack.spacing = 7
let imageContainerStack = UIStackView(arrangedSubviews: [buffer(), iconImageView, buffer()])
imageContainerStack.axis = .vertical
imageContainerStack.distribution = .fillEqually
addSubview(containerStack)
containerStack.anchors.top.pin()
containerStack.anchors.bottom.pin()
containerStack.anchors.leading.pin(inset: 10)
containerStack.anchors.trailing.pin(inset: 10)
containerStack.addArrangedSubview(subscriptionPlanStack)
containerStack.addArrangedSubview(buffer())
containerStack.addArrangedSubview(imageContainerStack)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func buffer(_ color: UIColor = .clear) -> UIView {
let buf = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 10))
buf.translatesAutoresizingMaskIntoConstraints = false
buf.backgroundColor = color
buf.widthAnchor.constraint(greaterThanOrEqualToConstant: 10).isActive = true
buf.heightAnchor.constraint(greaterThanOrEqualToConstant: 40).isActive = true
return buf
}
func setSelected(_ isSelected: Bool) {
self.layer.borderWidth = isSelected ? 3 : 1
self.layer.borderColor = isSelected ? UIColor.white.cgColor : UIColor.gray.cgColor
self.iconImageView.image = isSelected ? UIImage(named: "fill-2") : UIImage(named: "grey-ellipse-1")
}
}