-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardHighlight.swift
219 lines (180 loc) · 7.36 KB
/
CardHighlight.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
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
//
// CardHighlight.swift
//
// Created by MYMAC on 4/11/17.
// Copyright © 2018 Apple. All rights reserved.
//
import UIKit
@IBDesignable open class CardHighlight: Card {
/**
Text of the title label.
*/
@IBInspectable public var title: String = "welcome \nto \ncards !" {
didSet{
titleLbl.text = title.uppercased()
titleLbl.lineHeight(0.70)
}
}
/**
Max font size the title label.
*/
@IBInspectable public var titleSize:CGFloat = 26
/**
Text of the title label of the item at the bottom.
*/
@IBInspectable public var itemTitle: String = "Flappy Bird" {
didSet{
itemTitleLbl.text = itemTitle
}
}
/**
Max font size the subtitle label of the item at the bottom.
*/
@IBInspectable public var itemTitleSize: CGFloat = 16
/**
Text of the subtitle label of the item at the bottom.
*/
@IBInspectable public var itemSubtitle: String = "Flap that !" {
didSet{
itemSubtitleLbl.text = itemSubtitle
}
}
/**
Max font size the subtitle label of the item at the bottom.
*/
@IBInspectable public var itemSubtitleSize: CGFloat = 14
/**
Image displayed in the icon ImageView.
*/
@IBInspectable public var icon: UIImage? {
didSet{
iconIV.image = icon
bgIconIV.image = icon
}
}
/**
Corner radius for the icon ImageView
*/
@IBInspectable public var iconRadius: CGFloat = 16 {
didSet{
iconIV.layer.cornerRadius = iconRadius
bgIconIV.layer.cornerRadius = iconRadius*2
}
}
/**
Text for the card's button.
*/
@IBInspectable public var buttonText: String = "view" {
didSet{
self.setNeedsDisplay()
}
}
//Priv Vars
private var iconIV = UIImageView()
private var actionBtn = UIButton()
private var titleLbl = UILabel ()
private var itemTitleLbl = UILabel()
private var itemSubtitleLbl = UILabel()
private var lightColor = UIColor(red: 239/255, green: 239/255, blue: 244/255, alpha: 1)
private var bgIconIV = UIImageView()
fileprivate var btnWidth = CGFloat()
// View Life Cycle
override public init(frame: CGRect) {
super.init(frame: frame)
initialize()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initialize()
}
override func initialize() {
super.initialize()
actionBtn.addTarget(self, action: #selector(buttonTapped), for: UIControlEvents.touchUpInside)
backgroundIV.addSubview(iconIV)
backgroundIV.addSubview(titleLbl)
backgroundIV.addSubview(itemTitleLbl)
backgroundIV.addSubview(itemSubtitleLbl)
backgroundIV.addSubview(actionBtn)
if backgroundImage == nil { backgroundIV.addSubview(bgIconIV); }
else { bgIconIV.alpha = 0 }
}
override open func draw(_ rect: CGRect) {
super.draw(rect)
//Draw
bgIconIV.image = icon
bgIconIV.alpha = backgroundImage != nil ? 0 : 0.6
bgIconIV.clipsToBounds = true
iconIV.image = icon
iconIV.clipsToBounds = true
titleLbl.text = title.uppercased()
titleLbl.textColor = textColor
titleLbl.font = UIFont.systemFont(ofSize: titleSize, weight: .heavy)
titleLbl.adjustsFontSizeToFitWidth = true
titleLbl.lineHeight(0.70)
titleLbl.minimumScaleFactor = 0.1
titleLbl.lineBreakMode = .byTruncatingTail
titleLbl.numberOfLines = 3
backgroundIV.bringSubview(toFront: titleLbl)
itemTitleLbl.textColor = textColor
itemTitleLbl.text = itemTitle
itemTitleLbl.font = UIFont.boldSystemFont(ofSize: itemTitleSize)
itemTitleLbl.adjustsFontSizeToFitWidth = true
itemTitleLbl.minimumScaleFactor = 0.1
itemTitleLbl.lineBreakMode = .byTruncatingTail
itemTitleLbl.numberOfLines = 0
itemSubtitleLbl.textColor = textColor
itemSubtitleLbl.text = itemSubtitle
itemSubtitleLbl.font = UIFont.systemFont(ofSize: itemSubtitleSize)
itemSubtitleLbl.adjustsFontSizeToFitWidth = true
itemSubtitleLbl.minimumScaleFactor = 0.1
itemSubtitleLbl.lineBreakMode = .byTruncatingTail
itemSubtitleLbl.numberOfLines = 2
itemSubtitleLbl.sizeToFit()
actionBtn.backgroundColor = UIColor.clear
actionBtn.layer.backgroundColor = lightColor.cgColor
actionBtn.clipsToBounds = true
let btnTitle = NSAttributedString(string: buttonText.uppercased(), attributes: [ NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16, weight: .black), NSAttributedStringKey.foregroundColor : self.tintColor])
actionBtn.setAttributedTitle(btnTitle, for: .normal)
btnWidth = CGFloat((buttonText.count + 2) * 10)
layout(isPresenting: false)
}
override func layout(animating: Bool = true, isPresenting: Bool) {
super.layout(animating: animating, isPresenting: isPresenting)
let gimme = LayoutHelper(rect: backgroundIV.frame)
iconIV.frame = CGRect(x: insets,
y: insets,
width: gimme.Y(25),
height: gimme.Y(25))
titleLbl.frame.origin = CGPoint(x: insets, y: gimme.Y(5, from: iconIV))
titleLbl.frame.size.width = (originalFrame.width * 0.65) + ((backgroundIV.bounds.width - originalFrame.width)/3)
titleLbl.frame.size.height = gimme.Y(35)
itemSubtitleLbl.sizeToFit()
itemSubtitleLbl.frame.origin = CGPoint(x: insets, y: gimme.RevY(0, height: itemSubtitleLbl.bounds.size.height) - insets)
itemTitleLbl.frame = CGRect(x: insets,
y: gimme.RevY(0, height: gimme.Y(9), from: itemSubtitleLbl),
width: gimme.X(80) - btnWidth,
height: gimme.Y(9))
bgIconIV.transform = CGAffineTransform.identity
iconIV.layer.cornerRadius = iconRadius
bgIconIV.frame.size = CGSize(width: iconIV.bounds.width * 2, height: iconIV.bounds.width * 2)
bgIconIV.frame.origin = CGPoint(x: gimme.RevX(0, width: bgIconIV.frame.width) + LayoutHelper.Width(40, of: bgIconIV) , y: 0)
bgIconIV.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi/6))
bgIconIV.layer.cornerRadius = iconRadius * 2
actionBtn.frame = CGRect(x: gimme.RevX(0, width: btnWidth) - insets,
y: gimme.RevY(0, height: 32) - insets,
width: btnWidth,
height: 32)
actionBtn.layer.cornerRadius = actionBtn.layer.bounds.height/2
}
//Actions
@objc func buttonTapped(){
UIView.animate(withDuration: 0.2, animations: {
self.actionBtn.transform = CGAffineTransform(scaleX: 0.90, y: 0.90)
}) { _ in
UIView.animate(withDuration: 0.1, animations: {
self.actionBtn.transform = CGAffineTransform.identity
})
}
delegate?.cardHighlightDidTapButton?(card: self, button: actionBtn)
}
}