-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPUIText.swift
206 lines (183 loc) · 6.76 KB
/
PUIText.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
//
// PureUI
// PUIText.swift
//
// Copyright © 2017 Kenan Atmaca. All rights reserved.
// kenanatmaca.com
//
import UIKit
@IBDesignable
public class PUIText: UITextField {
private var bottomFrame:CALayer = CALayer()
private var placeholderAttrStr:NSMutableAttributedString!
private var spaceView:UIView = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func layoutSubviews() {
super.layoutSubviews()
}
// Border Width
@IBInspectable var borderWidth:CGFloat = 0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
// Border Color
@IBInspectable var borderColor:UIColor = UIColor.clear {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
// Corner Radius
@IBInspectable var radius:CGFloat = 0.0 {
didSet {
self.layer.cornerRadius = radius
}
}
// Mask to Bounds
@IBInspectable var maskBounds:Bool = false {
didSet {
self.layer.masksToBounds = maskBounds
}
}
// Clips to Bounds
@IBInspectable var clipsBounds:Bool = false {
didSet {
self.clipsToBounds = clipsBounds
}
}
// Shadow Color
@IBInspectable var shadowColor:UIColor = UIColor.clear {
didSet {
self.layer.shadowColor = shadowColor.cgColor
}
}
// Shadow Offset
@IBInspectable var shadowOffset:CGSize = CGSize(width: 0.0, height: 0.0) {
didSet {
self.layer.shadowOffset = shadowOffset
}
}
// Shadow Radius
@IBInspectable var shadowRadius:CGFloat = 0 {
didSet {
self.layer.shadowRadius = shadowRadius
}
}
// Shadow Opacity
@IBInspectable var shadowOpacity:Float = 0 {
didSet {
self.layer.shadowOpacity = shadowOpacity
}
}
// Left padding text
@IBInspectable var leftPadding:CGFloat = 0 {
didSet {
let paddingView = UIView()
paddingView.frame = CGRect(x: 0, y: 0, width: leftPadding, height: self.frame.height)
self.leftView = paddingView
self.leftViewMode = .always
}
}
// Right padding text
@IBInspectable var rightPadding:CGFloat = 0 {
didSet {
let paddingView = UIView()
paddingView.frame = CGRect(x: 0, y: 0, width: rightPadding, height: self.frame.height)
self.rightView = paddingView
self.rightViewMode = .always
}
}
// Bottom border set
@IBInspectable var onlyBottomBorder:Bool = false {
didSet {
if onlyBottomBorder {
spaceView.backgroundColor = UIColor.clear
bottomFrame.frame = CGRect(x: 0, y: self.frame.height - 1, width: self.frame.width, height: 2)
bottomFrame.backgroundColor = UIColor.red.cgColor
self.borderStyle = .none
self.layer.addSublayer(bottomFrame)
}
}
}
// Bottom border width
@IBInspectable var bottomWidth:CGFloat = 2.0 {
didSet {
bottomFrame.frame = CGRect(x: 0, y: self.frame.height - 1, width: self.frame.width, height: bottomWidth)
}
}
// Bottom border color
@IBInspectable var bottomColor:UIColor = UIColor.red {
didSet {
bottomFrame.backgroundColor = bottomColor.cgColor
}
}
// Left imageview set
@IBInspectable var leftImage:UIImage? = nil {
didSet {
let contentView:UIView = UIView()
contentView.frame = CGRect(x: 0, y: 0, width: 44, height: self.frame.height)
contentView.backgroundColor = UIColor.clear
spaceView = UIView()
spaceView.backgroundColor = UIColor().colorHex("#F3F3F3")
spaceView.frame = CGRect(x: 0, y: 0, width: 40, height: self.frame.height)
let leftImgView = UIImageView()
leftImgView.frame = CGRect(x: spaceView.frame.width / 2 - 12, y: spaceView.frame.height / 2 - 12, width: 24, height: 24)
leftImgView.image = leftImage
spaceView.addSubview(leftImgView)
contentView.addSubview(spaceView)
self.leftView = contentView
self.leftViewMode = .always
}
}
// Right imageview set
@IBInspectable var rightImage:UIImage? = nil {
didSet {
let contentView:UIView = UIView()
contentView.frame = CGRect(x: 0, y: 0, width: 44, height: self.frame.height)
contentView.backgroundColor = UIColor.clear
spaceView = UIView()
spaceView.backgroundColor = UIColor().colorHex("#F3F3F3")
spaceView.frame = CGRect(x: 4, y: 0, width: 40, height: self.frame.height)
let rightImgView = UIImageView()
rightImgView.frame = CGRect(x: spaceView.frame.width / 2 - 12, y: spaceView.frame.height / 2 - 12, width: 24, height: 24)
rightImgView.image = rightImage
spaceView.addSubview(rightImgView)
contentView.addSubview(spaceView)
self.rightView = contentView
self.rightViewMode = .always
}
}
// ImageView background color
@IBInspectable var imageColor:UIColor = UIColor().colorHex("#F3F3F3") {
didSet {
spaceView.backgroundColor = imageColor
}
}
// Placeholder
@IBInspectable var placeholderText:String = "" {
didSet {
placeholderAttrStr = NSMutableAttributedString(string: placeholderText, attributes: [NSFontAttributeName: UIFont(name: "Avenir", size: 15)!])
placeholderAttrStr.addAttributes([NSForegroundColorAttributeName: UIColor.lightGray.withAlphaComponent(0.7)], range: NSRange(location: 0,length: placeholderText.characters.count))
self.attributedPlaceholder = placeholderAttrStr
}
}
// Placeholder color
@IBInspectable var placeholderColor:UIColor = UIColor.lightGray.withAlphaComponent(0.8) {
didSet {
placeholderAttrStr.addAttributes([NSForegroundColorAttributeName: placeholderColor], range: NSRange(location: 0, length: placeholderText.characters.count))
self.attributedPlaceholder = placeholderAttrStr
}
}
// Placeholder font
var placeholderFont:UIFont = UIFont(name: "Avenir", size: 18)! {
didSet {
placeholderAttrStr.addAttributes([NSFontAttributeName: placeholderFont], range: NSRange(location: 0, length: placeholderText.characters.count))
self.attributedPlaceholder = placeholderAttrStr
}
}
}//