This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InformationView.swift
132 lines (116 loc) · 4.88 KB
/
InformationView.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
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
//
// InformationView.swift
// cuHacking
//
// Created by Santos on 2019-11-24.
// Copyright © 2019 cuHacking. All rights reserved.
//
import UIKit
class InformationView: UIView {
private static let visisbleButtonTopAnchorConstant: CGFloat = 10
private static let invisisbleButtonTopAnchorConstant: CGFloat = -20
private var buttonLeadingAnchor: NSLayoutConstraint!
private var buttonCenterXAnchor: NSLayoutConstraint!
private var buttonTopAnchor: NSLayoutConstraint!
var isCentered: Bool = false {
didSet {
if isCentered == true {
titleLabel.textAlignment = .center
informationTextView.textAlignment = .center
buttonLeadingAnchor.isActive = false
buttonCenterXAnchor.isActive = true
} else {
titleLabel.textAlignment = .left
informationTextView.textAlignment = .left
buttonLeadingAnchor.isActive = true
buttonCenterXAnchor.isActive = false
}
}
}
let titleLabel: UILabel = {
let label = UILabel()
label.textAlignment = .left
label.font = UIFont(font: Fonts.ReemKufi.regular, size: 17)
label.textColor = Asset.Colors.primaryText.color
return label
}()
let informationTextView: UITextView = {
let textView = UITextView()
textView.backgroundColor = nil
textView.textContainer.lineFragmentPadding = 0
textView.isScrollEnabled = false
textView.isEditable = false
textView.textAlignment = .left
textView.font = UIFont(font: Fonts.Mplus1p.medium, size: 12)
return textView
}()
let button: UIButton = {
let button = UIButton()
button.titleLabel?.font = UIFont(font: Fonts.Mplus1p.medium, size: 12)
button.setTitleColor(Asset.Colors.purple.color, for: .normal)
return button
}()
override init(frame: CGRect) {
super.init(frame: frame)
layer.cornerRadius = 4
translatesAutoresizingMaskIntoConstraints = false
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setup() {
let verticalStack = UIStackView()
verticalStack.translatesAutoresizingMaskIntoConstraints = false
verticalStack.axis = .vertical
verticalStack.layoutMargins = UIEdgeInsets(top: 20, left: 20, bottom: 0, right: 20)
verticalStack.isLayoutMarginsRelativeArrangement = true
verticalStack.addArrangedSubviews(views: titleLabel, informationTextView)
addSubview(verticalStack)
addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
verticalStack.leadingAnchor.constraint(equalTo: leadingAnchor),
verticalStack.topAnchor.constraint(equalTo: topAnchor),
verticalStack.trailingAnchor.constraint(equalTo: trailingAnchor)
])
buttonLeadingAnchor = button.leadingAnchor.constraint(equalTo: verticalStack.leadingAnchor, constant: 20)
buttonCenterXAnchor = button.centerXAnchor.constraint(equalTo: centerXAnchor)
buttonTopAnchor = button.topAnchor.constraint(equalTo: verticalStack.bottomAnchor, constant: 10)
buttonTopAnchor.isActive = true
buttonLeadingAnchor.isActive = true
if let lastSubview = subviews.last {
bottomAnchor.constraint(equalTo: lastSubview.bottomAnchor, constant: 10).isActive = true
}
}
func styleButton(textColor: UIColor? = nil, backgroundColor: UIColor? = nil) {
if let textColor = textColor {
button.setTitleColor(textColor, for: .normal)
button.setTitleColor(textColor, for: .selected)
}
if let backgroundColor = backgroundColor {
button.backgroundColor = backgroundColor
}
}
func update(title: String? = nil, information: String? = nil, buttonTitle: String? = nil, buttonIcon: UIImage? = nil) {
titleLabel.text = title
informationTextView.text = information
if let buttonTitle = buttonTitle {
if let buttonIcon = buttonIcon {
button.tintColor = Asset.Colors.background.color
button.setImage(buttonIcon, for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 16)
}
button.setTitle(buttonTitle, for: .normal)
buttonTopAnchor.constant = InformationView.visisbleButtonTopAnchorConstant
} else {
button.setTitle(nil, for: .normal)
buttonTopAnchor.constant = InformationView.invisisbleButtonTopAnchorConstant
}
}
func addTarget(target: Any?, action: Selector, for event: UIControl.Event) {
button.addTarget(target, action: #selector(ok), for: event)
}
@objc func ok() {
}
}