-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMaterialTextField.swift
109 lines (91 loc) · 2.92 KB
/
MaterialTextField.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
//
// MaterialTextField.swift
//
// Created by Andrey Yarosh on 07/08/2017.
// https://github.com/bingosoft/swift/tree/master/MaterialTextField
//
import Foundation
import UIKit
@IBDesignable
class MaterialTextField : UITextField
{
private var placeHolderLabel = UILabel();
private var underLineView = UIView();
private (set) var placeholderMinimized = false {
didSet {
guard (oldValue != placeholderMinimized) else { return }
let transform: CGAffineTransform!
if (placeholderMinimized) {
let k: CGFloat = 0.7;
let dx = placeHolderLabel.frame.width * (1 - k) / 2;
transform = CGAffineTransform(translationX: -dx, y: -20).scaledBy(x: k, y: k);
} else {
transform = .identity;
}
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut, animations: {
self.placeHolderLabel.layer.setAffineTransform(transform);
})
}
}
private var isActive = false;
override var text: String? {
didSet {
updateState();
}
}
@IBInspectable override var placeholder: String! {
set {
placeHolderLabel.text = newValue;
placeHolderLabel.sizeToFit();
}
get {
return placeHolderLabel.text
}
}
@IBInspectable var placeholderColor: UIColor! {
didSet {
placeHolderLabel.textColor = placeholderColor;
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
clipsToBounds = false;
tintColor = textColor;
placeholderColor = .gray;
addSubview(placeHolderLabel);
placeHolderLabel.translatesAutoresizingMaskIntoConstraints = false;
placeHolderLabel.font = font;
addConstraint(placeHolderLabel.leadingAnchor.constraint(equalTo: leadingAnchor));
addConstraint(placeHolderLabel.centerYAnchor.constraint(equalTo: centerYAnchor));
addSubview(underLineView);
underLineView.translatesAutoresizingMaskIntoConstraints = false;
underLineView.backgroundColor = UIColor.white.withAlphaComponent(0.6);
var c = NSLayoutConstraint.constraints(withVisualFormat: "|[v]|", options: .alignAllFirstBaseline, metrics: nil, views: ["v": underLineView])
addConstraints(c);
c = NSLayoutConstraint.constraints(withVisualFormat: "V:[v(2)]-(-3)-|", options: .alignAllFirstBaseline, metrics: nil, views: ["v": underLineView])
addConstraints(c);
addTarget(self, action: #selector(didStartEditing), for: .editingDidBegin);
addTarget(self, action: #selector(didEndEditing), for: .editingDidEnd);
addTarget(self, action: #selector(editingChanged), for: .editingChanged);
}
@objc func didStartEditing() {
UIView.animate(withDuration: 0.3) {
self.underLineView.backgroundColor = UIColor.white;
}
isActive = true;
updateState();
}
@objc func didEndEditing() {
UIView.animate(withDuration: 0.3) {
self.underLineView.backgroundColor = UIColor.white.withAlphaComponent(0.6);
}
isActive = false;
updateState();
}
@objc func editingChanged() {
updateState();
}
func updateState() {
placeholderMinimized = isActive || !text!.isEmpty
}
}