-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwiftyTextfields.swift
159 lines (129 loc) · 4.43 KB
/
SwiftyTextfields.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
//
// SwiftyTextfields.swift
// SwiftyTextfields
//
// Created by Dimitrios Kalaitzidis on 18/12/2016.
// Copyright © 2016 Dimitrios Kalaitzidis. All rights reserved.
//
import UIKit
@IBDesignable class SwiftyTextfields: UITextField{
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layoutSubviews()
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layoutSubviews()
}
}
@IBInspectable var borderColor: UIColor = UIColor.clear {
didSet {
layoutSubviews()
}
}
@IBInspectable var leftImage: UIImage? {
didSet {
updateView()
}
}
@IBInspectable var leftImageWidthHeight:CGRect = CGRect(x:0, y:0, width:14, height:14) {
didSet {
updateView()
}
}
@IBInspectable var bgColor: UIColor = UIColor.white {
didSet {
layoutSubviews()
}
}
@IBInspectable var placeholderColor: UIColor = UIColor.lightGray {
didSet {
updatePlaceholder()
}
}
@IBInspectable var enableShadow: Bool = false {
didSet {
layoutSubviews()
}
}
@IBInspectable var shadowColor: UIColor = UIColor.darkGray {
didSet {
layoutSubviews()
}
}
@IBInspectable var shadowRadius: CGFloat = 1 {
didSet {
layoutSubviews()
}
}
@IBInspectable var shadowOpacity: Float = 0.5 {
didSet {
layoutSubviews()
}
}
@IBInspectable var optionalPlaceholder: String = "" {
didSet {
updatePlaceholder()
}
}
@IBInspectable var leftPadding: CGFloat = 0
@IBInspectable var textfieldHeight: CGFloat = 30
override func layoutSubviews() {
super.layoutSubviews()
var frameRect = self.frame
frameRect.size.height = self.textfieldHeight
self.frame = frameRect
layer.backgroundColor = bgColor.cgColor
layer.cornerRadius = cornerRadius
layer.borderColor = borderColor.cgColor
layer.borderWidth = borderWidth
if(enableShadow == true){
let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height))
layer.shadowColor = shadowColor.cgColor
layer.shadowOffset = CGSize(width: 0.2, height: 0.2)
layer.shadowOpacity = shadowOpacity
layer.shadowRadius = shadowRadius
layer.masksToBounds = false
layer.shadowPath = shadowPath.cgPath
}
}
func updateView() {
if let image = leftImage {
leftViewMode = UITextFieldViewMode.always
let imageView = UIImageView(frame: leftImageWidthHeight)
imageView.image = image
imageView.tintColor = UIColor.clear
leftView = imageView
} else {
leftViewMode = UITextFieldViewMode.never
leftView = nil
}
}
func updatePlaceholder(){
// Placeholder text color
let placeholderText = NSAttributedString(string: placeholder != nil ? placeholder! : "", attributes:[NSForegroundColorAttributeName: placeholderColor])
attributedPlaceholder = placeholderText
if(optionalPlaceholder.characters.count > 0){
let optionalPlaceholderText = NSAttributedString(string: optionalPlaceholder, attributes:[NSFontAttributeName:UIFont.italicSystemFont(ofSize: 10)])
let combination = NSMutableAttributedString()
combination.append(placeholderText)
combination.append(optionalPlaceholderText)
attributedPlaceholder = combination
}
}
// Padding for images
override func leftViewRect(forBounds bounds: CGRect) -> CGRect {
var textRect = super.leftViewRect(forBounds: bounds)
textRect.origin.x += leftPadding
return textRect
}
// Padding for placeholder
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: leftImageWidthHeight.width + leftPadding * 2, dy: 0)
}
// Padding for text
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: leftImageWidthHeight.width + leftPadding * 2, dy: 0)
}
}