-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMKTextField.swift
192 lines (163 loc) · 5.28 KB
/
MKTextField.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
//
// MKTextField.swift
// TODO
//
// Created by Максим Хюттюля on 17/07/2019.
// Copyright © 2019 Максим Хюттюля. All rights reserved.
//
import UIKit
@IBDesignable
class MKTextField : UITextField, UITextFieldDelegate {
private let gradient : CAGradientLayer = CAGradientLayer();
private let iconImageView : UIImageView = UIImageView();
private let underLine : CALayer = CALayer();
@IBInspectable var cornerRadius : CGFloat = 0 {
didSet{
if (cornerRadius < 0){
cornerRadius = 0;
}
setupCorners()
}
}
@IBInspectable var borderWidth : CGFloat = 0.0 {
didSet{
if (borderWidth < 0){
borderWidth = 0.0;
}
setupCorners()
}
}
@IBInspectable var borderColor : UIColor = UIColor.black {
didSet{
setupCorners()
}
}
@IBInspectable var enableGradient : Bool = false {
didSet {
setupBackgroundColor();
}
}
@IBInspectable var firstColor : UIColor = UIColor.white {
didSet{
setupBackgroundColor();
}
}
@IBInspectable var secondColor : UIColor = UIColor.black {
didSet{
setupBackgroundColor();
}
}
@IBInspectable var isVerticalGradient : Bool = false {
didSet{
setupBackgroundColor();
}
}
@IBInspectable var iconLeft : UIImage? = nil {
didSet{
setupIcons();
}
}
@IBInspectable var iconLeftSize : CGFloat = 10 {
didSet{
setupIcons();
}
}
@IBInspectable var underlineEnable : Bool = false {
didSet{
setupUnderLine();
}
}
@IBInspectable var underlineHeight : CGFloat = 0 {
didSet{
setupUnderLine();
}
}
@IBInspectable var underlineLeftMargin : CGFloat = 0.0 {
didSet{
setupUnderLine();
}
}
@IBInspectable var underlineRightMargin : CGFloat = 0.0 {
didSet{
setupUnderLine();
}
}
@IBInspectable var underlineBottomMargin : CGFloat = 0.0 {
didSet{
setupUnderLine();
}
}
@IBInspectable var underlineBasicColor : UIColor = UIColor.clear {
didSet{
setupUnderLine();
}
}
@IBInspectable var underlineEditingColor : UIColor = UIColor.clear {
didSet{
setupUnderLine();
}
}
override init(frame: CGRect) {
super.init(frame: frame);
setupTextField();
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder);
setupTextField();
}
private func setupTextField(){
self.addTarget(self, action: #selector(self.textFieldDidBeginEditing(_:)), for: UIControl.Event.editingDidBegin);
self.addTarget(self, action: #selector(self.textFieldDidEndEditing(_:)), for: UIControl.Event.editingDidEnd);
setupBackgroundColor();
setupCorners();
}
private func setupBackgroundColor(){
gradient.removeFromSuperlayer();
if enableGradient {
gradient.frame = bounds;
gradient.colors = [firstColor.cgColor, secondColor.cgColor];
if (isVerticalGradient){
gradient.startPoint = CGPoint(x: 0.5, y: 0)
gradient.endPoint = CGPoint(x: 0.5, y: 1)
}else{
gradient.startPoint = CGPoint(x: 0, y: 0.5)
gradient.endPoint = CGPoint(x: 1, y: 0.5)
}
self.layer.insertSublayer(gradient, at: 0);
}
}
private func setupCorners(){
layer.borderWidth = borderWidth;
layer.cornerRadius = cornerRadius;
layer.borderColor = borderColor.cgColor;
gradient.cornerRadius = self.cornerRadius;
}
private func setupIcons(){
if (iconLeft != nil){
let x = self.bounds.height/2 - iconLeftSize/2;
let y = self.bounds.height/2 - iconLeftSize/2;
let iconView = UIImageView(frame:
CGRect(x: x, y: y, width: iconLeftSize, height: iconLeftSize))
iconView.image = iconLeft
let iconContainerView: UIView = UIView(frame:
CGRect(x: x, y: y, width: iconLeftSize+10, height: iconLeftSize))
iconContainerView.addSubview(iconView)
leftView = iconContainerView
leftViewMode = .always
}
}
private func setupUnderLine(){
underLine.removeFromSuperlayer();
if (underlineEnable){
underLine.frame = CGRect(x: underlineLeftMargin, y: self.bounds.height - underlineBottomMargin, width: self.bounds.width - underlineRightMargin - underlineLeftMargin, height: underlineHeight)
underLine.backgroundColor = underlineBasicColor.cgColor;
self.layer.addSublayer(underLine);
}
}
@objc func textFieldDidBeginEditing(_ textField: UITextField) {
underLine.backgroundColor = underlineEditingColor.cgColor;
}
@objc func textFieldDidEndEditing(_ textField: UITextField) {
underLine.backgroundColor = underlineBasicColor.cgColor;
}
}