forked from vyasmayank43/CustomInputTextField
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomInputTextField.swift
More file actions
434 lines (303 loc) · 15.2 KB
/
CustomInputTextField.swift
File metadata and controls
434 lines (303 loc) · 15.2 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//
// CustomInputTextField.swift
// Demo
//
// Created by Mayank Vyas on 16/05/16.
// Copyright © 2016 MoonShine. All rights reserved.
//
import UIKit
public enum TextValidation: NSInteger {
case None
case Empty
case Email
case Custom
}
@IBDesignable class CustomInputTextField: UITextField {
// MARK: - Variables
var hintLabel: UILabel! = UILabel()
var hintLabelTopConst: NSLayoutConstraint!
var errorLabel: UILabel! = UILabel()
var dividerView: UIView! = UIView()
var textValidationType: TextValidation = TextValidation.None
var customPredicate: NSPredicate = NSPredicate(format: "self.length > 0")
// MARK: - Custom IBInspectable
@IBInspectable var hintTextColor: UIColor = UIColor.grayColor() {
didSet {
if !isFirstResponder() {
hintLabel.textColor = hintTextColor
}
}
}
@IBInspectable var hintActiveTextColor: UIColor = UIColor.grayColor() {
didSet {
if isFirstResponder() {
hintLabel.textColor = hintActiveTextColor
}
}
}
@IBInspectable var errorTextColor: UIColor = UIColor.grayColor() {
didSet {
if !isFirstResponder() {
errorLabel.textColor = errorTextColor
}
}
}
@IBInspectable var dividerColor: UIColor = UIColor.grayColor() {
didSet {
if !isFirstResponder() {
dividerView.backgroundColor = dividerColor
}
}
}
@IBInspectable var hintFontSize: CGFloat = 11.0 {
didSet {
hintLabel.font = UIFont(name: (self.font?.fontName)!, size: hintFontSize)
}
}
@IBInspectable var errorFontSize: CGFloat = 9.0 {
didSet {
errorLabel.font = UIFont(name: (self.font?.fontName)!, size: errorFontSize)
}
}
@IBInspectable var errorText: String = "" {
didSet {
errorLabel.text = errorText
}
}
// MARK:- Overrides
// #if !TARGET_INTERFACE_BUILDER
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.setupCustomView()
}
override init(frame:CGRect) {
super.init(frame:frame)
self.setupCustomView()
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil)
}
private func setupCustomView() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CustomInputTextField.updateViewLayout), name: UITextFieldTextDidChangeNotification, object: nil)
if self.tag != 0 {
let titleFont: UIFont = UIFont(name: (self.font?.fontName)!, size: 16)!
let activeAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor(red: 33/255.0, green: 150/255.0, blue: 243/255.0, alpha: 1.0)]
let keyBoardToolBar: UIToolbar = UIToolbar (frame: CGRectMake(0, 0, self.frame.size.width, 44))
keyBoardToolBar.tintColor = UIColor (red: 0.26, green: 0.77, blue: 0.43, alpha: 1.0)
let prevBarBtn: UIBarButtonItem = UIBarButtonItem (title: " Prev", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.prevBarBtnAction(_:)))
prevBarBtn.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
let nextBarBtn: UIBarButtonItem = UIBarButtonItem (title: " Next", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.nextBarBtnAction(_:)))
nextBarBtn.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
let flexibleSpaceBtn: UIBarButtonItem = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
let doneBtn: UIBarButtonItem = UIBarButtonItem (title: " Done", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.doneBarBtnAction(_:)))
doneBtn.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
let itemArray = [prevBarBtn, nextBarBtn, flexibleSpaceBtn, doneBtn]
keyBoardToolBar.setItems(itemArray, animated: true)
self.inputAccessoryView = keyBoardToolBar
}
hintLabel.alpha = 0.0
hintLabel.textAlignment = NSTextAlignment.Left
hintLabel.backgroundColor = UIColor.clearColor()
hintLabel.textColor = hintTextColor
hintLabel.text = self.placeholder
self.addSubview(hintLabel)
hintLabel.translatesAutoresizingMaskIntoConstraints = false
hintLabelTopConst = NSLayoutConstraint(item: hintLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0)
self.addConstraints([
hintLabelTopConst,
NSLayoutConstraint(item: hintLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: hintLabel, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0),
])
dividerView.alpha = 1.0
self.addSubview(dividerView)
dividerView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints([
NSLayoutConstraint(item: dividerView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: dividerView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: dividerView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 1.0)
])
errorLabel.alpha = 0.0
errorLabel.textAlignment = NSTextAlignment.Right
errorLabel.backgroundColor = UIColor.clearColor()
errorLabel.textColor = errorTextColor
self.addSubview(errorLabel)
errorLabel.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints([
NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: dividerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 2.0),
NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: errorLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0),
])
}
override var placeholder: String? {
didSet {
hintLabel.text = self.placeholder
}
}
override var font: UIFont? {
didSet {
hintLabel.font = UIFont(name: (font?.fontName)!, size: hintFontSize)
}
}
// #endif
override func layoutSubviews() {
super.layoutSubviews()
self.updateViewLayout()
}
func updateViewLayout() {
let isResp = self.isFirstResponder()
if isResp && self.text!.isEmpty == false {
hintLabel.textColor = hintActiveTextColor
} else {
hintLabel.textColor = hintTextColor
}
// Should we show or hide the title label?
if self.text!.isEmpty == false {
// Hide
self.showHintLabel()
} else {
// Show
self.hideHintLabel()
}
if let toolbar: UIToolbar = self.inputAccessoryView as? UIToolbar {
let titleFont: UIFont = UIFont(name: (self.font?.fontName)!, size: 16)!
let activeAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor(red: 33/255.0, green: 150/255.0, blue: 243/255.0, alpha: 1.0)]
let inactiveAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor.lightGrayColor()]
let prevBarBtn: UIBarButtonItem = toolbar.items![0]
let nextBarBtn: UIBarButtonItem = toolbar.items![1]
let prevTextField = self.superview?.viewWithTag(self.tag - 1)
if prevTextField is CustomInputTextField || prevTextField is CustomInputTextView {
prevBarBtn.enabled = true
prevBarBtn.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
}
else {
prevBarBtn.enabled = false
prevBarBtn.setTitleTextAttributes(inactiveAttrs, forState: UIControlState.Normal)
}
let nextTextField = self.superview?.viewWithTag(self.tag + 1)
if nextTextField is CustomInputTextField || nextTextField is CustomInputTextView {
nextBarBtn.enabled = true
nextBarBtn.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
}
else {
nextBarBtn.enabled = false
nextBarBtn.setTitleTextAttributes(inactiveAttrs, forState: UIControlState.Normal)
}
}
}
override func textRectForBounds(bounds:CGRect) -> CGRect {
var r = super.textRectForBounds(bounds)
r.origin.x = r.origin.x + 5
r.size.width = r.size.width - 10
return r
}
override func editingRectForBounds(bounds:CGRect) -> CGRect {
var r = super.editingRectForBounds(bounds)
r.origin.x = r.origin.x + 5
r.size.width = r.size.width - 10
return r
}
// MARK: Private Methods
private func showHintLabel() {
hintLabelTopConst.constant = 0.0
self.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.3) {
self.layoutIfNeeded()
self.hintLabel.alpha = 1.0
}
}
private func hideHintLabel() {
hintLabelTopConst.constant = self.hintLabel.font.lineHeight + 5
self.setNeedsUpdateConstraints()
UIView.animateWithDuration(0.3) {
self.layoutIfNeeded()
self.hintLabel.alpha = 0.0
}
}
private func showErrorLabel() {
UIView.animateWithDuration(0.3) {
self.errorLabel.alpha = 1.0
}
}
private func hideErrorLabel() {
UIView.animateWithDuration(0.3) {
self.errorLabel.alpha = 0.0
}
}
// MARK: - IBActions
func prevBarBtnAction (sender: UIBarButtonItem) {
self.validateText()
let titleFont: UIFont = UIFont(name: (self.font?.fontName)!, size: 16)!
let activeAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor(red: 33/255.0, green: 150/255.0, blue: 243/255.0, alpha: 1.0)]
let inactiveAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor.lightGrayColor()]
let prevTextField = self.superview?.viewWithTag(self.tag - 1)
if prevTextField is CustomInputTextField || prevTextField is CustomInputTextView {
prevTextField?.becomeFirstResponder()
sender.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
}
else {
sender.enabled = false
sender.setTitleTextAttributes(inactiveAttrs, forState: UIControlState.Normal)
}
}
func nextBarBtnAction (sender: UIBarButtonItem) {
self.validateText()
let titleFont: UIFont = UIFont(name: (self.font?.fontName)!, size: 16)!
let activeAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor(red: 33/255.0, green: 150/255.0, blue: 243/255.0, alpha: 1.0)]
let inactiveAttrs = [NSFontAttributeName : titleFont, NSForegroundColorAttributeName : UIColor.lightGrayColor()]
let nextTextField = self.superview?.viewWithTag(self.tag + 1)
if nextTextField is CustomInputTextField || nextTextField is CustomInputTextView {
nextTextField?.becomeFirstResponder()
sender.setTitleTextAttributes(activeAttrs, forState: UIControlState.Normal)
}
else {
sender.enabled = false
sender.setTitleTextAttributes(inactiveAttrs, forState: UIControlState.Normal)
}
}
func doneBarBtnAction (sender: UIBarButtonItem) {
if let scrollView: UIScrollView = self.superview?.superview as? UIScrollView {
scrollView.setContentOffset(CGPointMake(scrollView.contentOffset.x, 0), animated: true)
}
self.resignFirstResponder()
self.validateText()
}
func validateText() -> Bool {
switch textValidationType {
case .Empty:
let result = customPredicate.evaluateWithObject(self.text)
if result == false {
self.showErrorLabel()
}
else {
self.hideErrorLabel()
}
return result
case TextValidation.Email:
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
let result = emailTest.evaluateWithObject(self.text)
if result == false {
self.showErrorLabel()
}
else {
self.hideErrorLabel()
}
return result
case .Custom:
let result = customPredicate.evaluateWithObject(self.text)
if result == false {
self.showErrorLabel()
}
else {
self.hideErrorLabel()
}
return result
default:
return true
}
}
func hideValidation() {
self.hideErrorLabel()
}
}