forked from EurekaCommunity/FloatLabelRow
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFloatLabelRow.swift
504 lines (379 loc) · 16.9 KB
/
FloatLabelRow.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// FloatLabelRow.swift
// Eureka ( https://github.com/xmartlabs/Eureka )
//
// Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import UIKit
import MapKit
import Eureka
//MARK: FloatLabelCell
public class _FloatLabelCell<T>: Cell<T>, UITextFieldDelegate, TextFieldCell where T: Equatable, T: InputTypeInitiable {
public var textField: UITextField! { return floatLabelTextField }
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy public var floatLabelTextField: FloatLabelTextField = { [unowned self] in
let floatTextField = FloatLabelTextField()
floatTextField.translatesAutoresizingMaskIntoConstraints = false
floatTextField.font = .preferredFont(forTextStyle: .body)
floatTextField.titleFont = .boldSystemFont(ofSize: 11.0)
floatTextField.clearButtonMode = .whileEditing
return floatTextField
}()
open override func setup() {
super.setup()
height = { 55 }
selectionStyle = .none
contentView.addSubview(floatLabelTextField)
floatLabelTextField.delegate = self
floatLabelTextField.addTarget(self, action: #selector(_FloatLabelCell.textFieldDidChange(_:)), for: .editingChanged)
contentView.addConstraints(layoutConstraints())
}
open override func update() {
super.update()
textLabel?.text = nil
detailTextLabel?.text = nil
floatLabelTextField.attributedPlaceholder = NSAttributedString(string: row.title ?? "", attributes: [NSForegroundColorAttributeName: UIColor.lightGray])
floatLabelTextField.text = row.displayValueFor?(row.value)
floatLabelTextField.isEnabled = !row.isDisabled
floatLabelTextField.titleTextColour = .lightGray
floatLabelTextField.alpha = row.isDisabled ? 0.6 : 1
}
open override func cellCanBecomeFirstResponder() -> Bool {
return !row.isDisabled && floatLabelTextField.canBecomeFirstResponder
}
open override func cellBecomeFirstResponder(withDirection direction: Direction) -> Bool {
return floatLabelTextField.becomeFirstResponder()
}
open override func cellResignFirstResponder() -> Bool {
return floatLabelTextField.resignFirstResponder()
}
private func layoutConstraints() -> [NSLayoutConstraint] {
let views = ["floatLabeledTextField": floatLabelTextField]
let metrics = ["vMargin":8.0]
return NSLayoutConstraint.constraints(withVisualFormat: "H:|-[floatLabeledTextField]-|", options: .alignAllLastBaseline, metrics: metrics, views: views) + NSLayoutConstraint.constraints(withVisualFormat: "V:|-(vMargin)-[floatLabeledTextField]-(vMargin)-|", options: .alignAllLastBaseline, metrics: metrics, views: views)
}
open func textFieldDidChange(_ textField: UITextField) {
guard let textValue = textField.text else {
row.value = nil
return
}
guard let fieldRow = row as? FormatterConformance, let formatter = fieldRow.formatter else {
row.value = textValue.isEmpty ? nil : (T.init(string: textValue) ?? row.value)
return
}
if fieldRow.useFormatterDuringInput {
let value: AutoreleasingUnsafeMutablePointer<AnyObject?> = AutoreleasingUnsafeMutablePointer<AnyObject?>.init(UnsafeMutablePointer<T>.allocate(capacity: 1))
let errorDesc: AutoreleasingUnsafeMutablePointer<NSString?>? = nil
if formatter.getObjectValue(value, for: textValue, errorDescription: errorDesc) {
row.value = value.pointee as? T
guard var selStartPos = textField.selectedTextRange?.start else { return }
let oldVal = textField.text
textField.text = row.displayValueFor?(row.value)
selStartPos = (formatter as? FormatterProtocol)?.getNewPosition(forPosition: selStartPos, inTextInput: textField, oldValue: oldVal, newValue: textField.text) ?? selStartPos
textField.selectedTextRange = textField.textRange(from: selStartPos, to: selStartPos)
return
}
} else {
let value: AutoreleasingUnsafeMutablePointer<AnyObject?> = AutoreleasingUnsafeMutablePointer<AnyObject?>.init(UnsafeMutablePointer<T>.allocate(capacity: 1))
let errorDesc: AutoreleasingUnsafeMutablePointer<NSString?>? = nil
if formatter.getObjectValue(value, for: textValue, errorDescription: errorDesc) {
row.value = value.pointee as? T
} else {
row.value = textValue.isEmpty ? nil : (T.init(string: textValue) ?? row.value)
}
}
}
//Mark: Helpers
private func displayValue(useFormatter: Bool) -> String? {
guard let v = row.value else { return nil }
if let formatter = (row as? FormatterConformance)?.formatter, useFormatter {
return textField?.isFirstResponder == true ? formatter.editingString(for: v) : formatter.string(for: v)
}
return String(describing: v)
}
//MARK: TextFieldDelegate
public func textFieldDidBeginEditing(_ textField: UITextField) {
formViewController()?.beginEditing(of: self)
if let fieldRowConformance = row as? FormatterConformance, let _ = fieldRowConformance.formatter, fieldRowConformance.useFormatterOnDidBeginEditing ?? fieldRowConformance.useFormatterDuringInput {
textField.text = displayValue(useFormatter: true)
} else {
textField.text = displayValue(useFormatter: false)
}
}
public func textFieldDidEndEditing(_ textField: UITextField) {
formViewController()?.endEditing(of: self)
formViewController()?.textInputDidEndEditing(textField, cell: self)
textFieldDidChange(textField)
textField.text = displayValue(useFormatter: (row as? FormatterConformance)?.formatter != nil)
}
public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return formViewController()?.textInputShouldReturn(textField, cell: self) ?? true
}
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return formViewController()?.textInput(textField, shouldChangeCharactersInRange:range, replacementString:string, cell: self) ?? true
}
public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return formViewController()?.textInputShouldBeginEditing(textField, cell: self) ?? true
}
public func textFieldShouldClear(_ textField: UITextField) -> Bool {
return formViewController()?.textInputShouldClear(textField, cell: self) ?? true
}
public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return formViewController()?.textInputShouldEndEditing(textField, cell: self) ?? true
}
}
public class TextFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.autocorrectionType = .default
textField?.autocapitalizationType = .sentences
textField?.keyboardType = .default
}
}
public class IntFloatLabelCell : _FloatLabelCell<Int>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.autocorrectionType = .default
textField?.autocapitalizationType = .none
textField?.keyboardType = .numberPad
}
}
public class DecimalFloatLabelCell : _FloatLabelCell<Double>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.keyboardType = .decimalPad
textField.autocorrectionType = .no
}
}
public class URLFloatLabelCell : _FloatLabelCell<URL>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.keyboardType = .URL
}
}
public class TwitterFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.autocorrectionType = .no
textField?.autocapitalizationType = .none
textField?.keyboardType = .twitter
}
}
public class AccountFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.autocorrectionType = .no
textField?.autocapitalizationType = .none
textField?.keyboardType = .asciiCapable
}
}
public class PasswordFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.autocorrectionType = .no
textField?.autocapitalizationType = .none
textField?.keyboardType = .asciiCapable
textField?.isSecureTextEntry = true
}
}
public class NameFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField.autocorrectionType = .no
textField.autocapitalizationType = .words
textField.keyboardType = .asciiCapable
}
}
public class EmailFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.autocorrectionType = .no
textField?.autocapitalizationType = .none
textField?.keyboardType = .emailAddress
}
}
public class PhoneFloatLabelCell : _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func setup() {
super.setup()
textField?.keyboardType = .phonePad
}
}
public class ZipCodeFloatLabelCell: _FloatLabelCell<String>, CellType {
required public init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open override func update() {
super.update()
textField.autocorrectionType = .no
textField.autocapitalizationType = .allCharacters
textField.keyboardType = .numbersAndPunctuation
}
}
// MARK: Rows with formatter
open class _IntFloatRow: FloatLabelRow<IntFloatLabelCell> {
public required init(tag: String?) {
super.init(tag: tag)
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale.current
numberFormatter.numberStyle = .decimal
numberFormatter.minimumFractionDigits = 0
formatter = numberFormatter
}
}
open class _DecimalFloatRow: FloatLabelRow<DecimalFloatLabelCell> {
public required init(tag: String?) {
super.init(tag: tag)
let numberFormatter = NumberFormatter()
numberFormatter.locale = Locale.current
numberFormatter.numberStyle = .decimal
numberFormatter.minimumFractionDigits = 2
formatter = numberFormatter
}
}
//MARK: FloatLabelRow
open class FloatLabelRow<Cell: CellType>: FormatteableRow<Cell> where Cell: BaseCell, Cell: TextFieldCell {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class TextFloatLabelRow: FloatLabelRow<TextFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class IntFloatLabelRow: _IntFloatRow, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class DecimalFloatLabelRow: _DecimalFloatRow, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class URLFloatLabelRow: FloatLabelRow<URLFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class TwitterFloatLabelRow: FloatLabelRow<TwitterFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class AccountFloatLabelRow: FloatLabelRow<AccountFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class PasswordFloatLabelRow: FloatLabelRow<PasswordFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class NameFloatLabelRow: FloatLabelRow<NameFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class EmailFloatLabelRow: FloatLabelRow<EmailFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class PhoneFloatLabelRow: FloatLabelRow<PhoneFloatLabelCell>, RowType {
public required init(tag: String?) {
super.init(tag: tag)
}
}
public final class ZipCodeFloatLabelRow: FloatLabelRow<ZipCodeFloatLabelCell>, RowType {
required public init(tag: String?) {
super.init(tag: tag)
}
}