-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathSkyFloatingLabelTextFieldWithIcon.swift
356 lines (316 loc) · 11.5 KB
/
SkyFloatingLabelTextFieldWithIcon.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2016-2019 Skyscanner Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.
import UIKit
/**
Identify the type of icon.
- font: Set your icon by setting the font of iconLabel
- image: Set your icon by setting the image of iconImageView
*/
public enum IconType: Int {
case font
case image
}
/**
A beautiful and flexible textfield implementation with support for icon, title label, error message and placeholder.
*/
@available(iOSApplicationExtension, unavailable)
open class SkyFloatingLabelTextFieldWithIcon: SkyFloatingLabelTextField {
@IBInspectable
var iconTypeValue: Int {
get {
return self.iconType.rawValue
}
set(iconIndex) {
self.iconType = IconType(rawValue: iconIndex) ?? .font
}
}
open var iconType: IconType = .font {
didSet {
updateIconViewHiddenState()
}
}
/// A UIImageView value that identifies the view used to display the icon
open var iconImageView: UIImageView!
/// A UIImage value that determines the image that the icon is using
@IBInspectable
dynamic open var iconImage: UIImage? {
didSet {
// Show a warning if setting an image while the iconType is IconType.font
if self.iconType == .font { NSLog("WARNING - Did set iconImage when the iconType is set to IconType.font. The image will not be displayed.") } // swiftlint:disable:this line_length
iconImageView?.image = iconImage
}
}
/// A Bool value that determines if the UIImage should be templated or not
@IBInspectable
dynamic open var templateImage: Bool = true {
didSet {
if templateImage {
let templatedOriginalImage = self.iconImageView.image?
.withRenderingMode(.alwaysTemplate)
self.iconImageView.image = templatedOriginalImage
}
}
}
/// A UILabel value that identifies the label used to display the icon
open var iconLabel: UILabel!
/// A UIFont value that determines the font that the icon is using
@objc dynamic open var iconFont: UIFont? {
didSet {
iconLabel?.font = iconFont
}
}
/// A String value that determines the text used when displaying the icon
@IBInspectable
open var iconText: String? {
didSet {
// Show a warning if setting an icon text while the iconType is IconType.image
if self.iconType == .image { NSLog("WARNING - Did set iconText when the iconType is set to IconType.image. The icon with the specified text will not be displayed.") } // swiftlint:disable:this line_length
iconLabel?.text = iconText
}
}
/// A UIColor value that determines the color of the icon in the normal state
@IBInspectable
dynamic open var iconColor: UIColor = UIColor.gray {
didSet {
if self.iconType == .font {
updateIconLabelColor()
}
if self.iconType == .image && self.templateImage {
updateImageViewTintColor()
}
}
}
/// A UIColor value that determines the color of the icon when the control is selected
@IBInspectable
dynamic open var selectedIconColor: UIColor = UIColor.gray {
didSet {
updateIconLabelColor()
}
}
/// A float value that determines the width of the icon
@IBInspectable
dynamic open var iconWidth: CGFloat = 20 {
didSet {
updateFrame()
}
}
/**
A float value that determines the left margin of the icon.
Use this value to position the icon more precisely horizontally.
*/
@IBInspectable
dynamic open var iconMarginLeft: CGFloat = 4 {
didSet {
updateFrame()
}
}
/**
A float value that determines the bottom margin of the icon.
Use this value to position the icon more precisely vertically.
*/
@IBInspectable
dynamic open var iconMarginBottom: CGFloat = 4 {
didSet {
updateFrame()
}
}
/**
A float value that determines the rotation in degrees of the icon.
Use this value to rotate the icon in either direction.
*/
@IBInspectable
open var iconRotationDegrees: Double = 0 {
didSet {
iconLabel.transform = CGAffineTransform(rotationAngle: CGFloat(iconRotationDegrees * .pi / 180.0))
iconImageView.transform = CGAffineTransform(rotationAngle: CGFloat(iconRotationDegrees * .pi / 180.0))
}
}
// MARK: Initializers
/**
Initializes the control
- parameter type the type of icon
*/
convenience public init(frame: CGRect, iconType: IconType) {
self.init(frame: frame)
self.iconType = iconType
updateIconViewHiddenState()
}
/**
Initializes the control
- parameter frame the frame of the control
*/
override public init(frame: CGRect) {
super.init(frame: frame)
createIcon()
updateIconViewHiddenState()
}
/**
Intialzies the control by deserializing it
- parameter aDecoder the object to deserialize the control from
*/
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
createIcon()
updateIconViewHiddenState()
}
// MARK: Creating the icon
/// Creates the both icon label and icon image view
fileprivate func createIcon() {
createIconLabel()
createIconImageView()
}
// MARK: Creating the icon label
/// Creates the icon label
fileprivate func createIconLabel() {
let iconLabel = UILabel()
iconLabel.backgroundColor = UIColor.clear
iconLabel.textAlignment = .center
iconLabel.autoresizingMask = [.flexibleTopMargin, .flexibleRightMargin]
self.iconLabel = iconLabel
addSubview(iconLabel)
updateIconLabelColor()
}
// MARK: Creating the icon image view
/// Creates the icon image view
fileprivate func createIconImageView() {
let iconImageView = UIImageView()
iconImageView.backgroundColor = .clear
iconImageView.contentMode = .scaleAspectFit
iconImageView.autoresizingMask = [.flexibleTopMargin, .flexibleRightMargin]
self.iconImageView = iconImageView
self.templateImage = true
addSubview(iconImageView)
}
// MARK: Set icon hidden property
/// Shows the corresponding icon depending on iconType property
fileprivate func updateIconViewHiddenState() {
switch iconType {
case .font:
self.iconLabel.isHidden = false
self.iconImageView.isHidden = true
case .image:
self.iconLabel.isHidden = true
self.iconImageView.isHidden = false
}
}
// MARK: Handling the icon color
/// Update the colors for the control. Override to customize colors.
override open func updateColors() {
super.updateColors()
if self.iconType == .font {
updateIconLabelColor()
}
if self.iconType == .image && self.templateImage {
updateImageViewTintColor()
}
}
fileprivate func updateImageViewTintColor() {
if !isEnabled {
iconImageView.tintColor = disabledColor
} else if hasErrorMessage {
iconImageView.tintColor = errorColor
} else {
iconImageView.tintColor = editingOrSelected ? selectedIconColor : iconColor
}
}
fileprivate func updateIconLabelColor() {
if !isEnabled {
iconLabel?.textColor = disabledColor
} else if hasErrorMessage {
iconLabel?.textColor = errorColor
} else {
iconLabel?.textColor = editingOrSelected ? selectedIconColor : iconColor
}
}
// MARK: Custom layout overrides
/**
Calculate the bounds for the textfield component of the control.
Override to create a custom size textbox in the control.
- parameter bounds: The current bounds of the textfield component
- returns: The rectangle that the textfield component should render in
*/
override open func textRect(forBounds bounds: CGRect) -> CGRect {
var rect = super.textRect(forBounds: bounds)
if isLTRLanguage {
rect.origin.x += CGFloat(iconWidth + iconMarginLeft)
} else {
rect.origin.x -= CGFloat(iconWidth + iconMarginLeft)
}
rect.size.width -= CGFloat(iconWidth + iconMarginLeft)
return rect
}
/**
Calculate the rectangle for the textfield when it is being edited
- parameter bounds: The current bounds of the field
- returns: The rectangle that the textfield should render in
*/
override open func editingRect(forBounds bounds: CGRect) -> CGRect {
var rect = super.editingRect(forBounds: bounds)
if isLTRLanguage {
rect.origin.x += CGFloat(iconWidth + iconMarginLeft)
} else {
// don't change the editing field X position for RTL languages
}
rect.size.width -= CGFloat(iconWidth + iconMarginLeft)
return rect
}
/**
Calculates the bounds for the placeholder component of the control.
Override to create a custom size textbox in the control.
- parameter bounds: The current bounds of the placeholder component
- returns: The rectangle that the placeholder component should render in
*/
override open func placeholderRect(forBounds bounds: CGRect) -> CGRect {
var rect = super.placeholderRect(forBounds: bounds)
if isLTRLanguage {
rect.origin.x += CGFloat(iconWidth + iconMarginLeft)
} else {
// don't change the editing field X position for RTL languages
}
rect.size.width -= CGFloat(iconWidth + iconMarginLeft)
return rect
}
/// Invoked by layoutIfNeeded automatically
override open func layoutSubviews() {
super.layoutSubviews()
updateFrame()
}
fileprivate func updateFrame() {
let textWidth: CGFloat = bounds.size.width
if isLTRLanguage {
iconLabel.frame = CGRect(
x: 0,
y: bounds.size.height - textHeight() - iconMarginBottom,
width: iconWidth,
height: textHeight()
)
iconImageView.frame = CGRect(
x: 0,
y: bounds.size.height - textHeight() - iconMarginBottom,
width: iconWidth,
height: textHeight()
)
} else {
iconLabel.frame = CGRect(
x: textWidth - iconWidth,
y: bounds.size.height - textHeight() - iconMarginBottom,
width: iconWidth,
height: textHeight()
)
iconImageView.frame = CGRect(
x: textWidth - iconWidth,
y: bounds.size.height - textHeight() - iconMarginBottom,
width: iconWidth,
height: textHeight()
)
}
}
}