Repository navigation
Expand file tree
/
Copy pathUITextBox.swift
More file actions
executable file
·261 lines (229 loc) · 9.33 KB
/
Copy pathUITextBox.swift
File metadata and controls
executable file
·261 lines (229 loc) · 9.33 KB
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
//
// UITextBox.swift
// UITest
//
// Created by 李招利 on 14/7/10.
// Copyright (c) 2014年 慧趣工作室. All rights reserved.
//
import UIKit
//enum UITextBoxContentType {
// case AnyChar
// case Number
// case Integer
// case EMail
// case Phone
// case Telephone
// case MobilePhone
// case CustomType
//}
enum UITextBoxHighlightState {
case Default
case Validator (String) // 状态提示文字
case Warning (String) // 状态提示文字
case Wrong (String) // 状态提示文字
}
@IBDesignable
class UITextBox: UITextField {
@IBInspectable var wrongColor:UIColor = UIColor(number: 0xFFEEEE) // 淡红色
@IBInspectable var warningColor:UIColor = UIColor(number: 0xFFFFCC) // 淡黄色
@IBInspectable var validatorColor:UIColor = UIColor(number: 0xEEFFEE) // 淡绿色
@IBInspectable var highlightColor:UIColor = UIColor(number: 0xEEF7FF) // 淡蓝色
@IBInspectable var animateDuration:CGFloat = 0.4
weak var placeholderLabel:UILabel?
@NSCopying private var _backgroundColor: UIColor? = nil
override var backgroundColor: UIColor? {
set {
_backgroundColor = newValue
super.backgroundColor = self.getHighlightColor(self.highlightState)
}
get {
return _backgroundColor
}
}
override var attributedPlaceholder: NSAttributedString? {
didSet {
if let label = placeholderLabel {
label.attributedText = super.attributedPlaceholder
self.layoutSubviews()
}
}
}
override var placeholder:String? {
didSet {
if let label = placeholderLabel {
label.text = super.placeholder
self.layoutSubviews()
}
}
}
private var _highlightState:UITextBoxHighlightState {
return text == nil || text == "" ? .Default : highlightState
}
var highlightState:UITextBoxHighlightState = .Default {
didSet {
if let label = placeholderLabel {
setHighlightText(label, state: _highlightState)
self.layoutSubviews()
}
UIView.animateWithDuration(NSTimeInterval(animateDuration)) {
super.backgroundColor = self.getHighlightColor(self._highlightState)
}
}
}
// override init(frame: CGRect) {
// super.init(frame: frame)
// self.addTarget(self, action: Selector("editingChanged"), forControlEvents: UIControlEvents.EditingChanged);
// }
//
// required init?(coder aDecoder: NSCoder) {
// super.init(coder: aDecoder)
// self.addTarget(self, action: Selector("editingChanged"), forControlEvents: UIControlEvents.EditingChanged);
// }
//
// func editingChanged() {
// print("editingChanged:\(text)")
// }
//获得焦点时高亮动画
override func becomeFirstResponder() -> Bool {
return animationFirstResponder(super.becomeFirstResponder())
}
//失去焦点时取消高亮动画
override func resignFirstResponder() -> Bool {
return animationFirstResponder(super.resignFirstResponder())
}
//
private func animationFirstResponder(isFirstResponder:Bool) -> Bool {
UIView.animateWithDuration(NSTimeInterval(animateDuration)) {
let color = self.getHighlightColor(self._highlightState)
super.backgroundColor = color
if let label = self.placeholderLabel {
self.setHighlightText(label, state: self._highlightState)
}
}
return isFirstResponder
}
//调整子控件布局
override func layoutSubviews() {
super.layoutSubviews()
let rect = super.placeholderRectForBounds(bounds)
if isFirstResponder() {
layoutPlaceholderLabel(rect,false)
} else if text == nil || text == "" {
layoutPlaceholderLabel(rect,true)
} else {
layoutPlaceholderLabel(rect,false)
}
}
override func willMoveToSuperview(newSuperview: UIView!) {
super.willMoveToSuperview(newSuperview)
if placeholderLabel == nil {
let rect = super.placeholderRectForBounds(bounds)
let label = UILabel(frame: rect)
label.font = self.font
setHighlightText(label, state: self._highlightState)
placeholderLabel = label
self.addSubview(label);
}
}
override func removeFromSuperview() {
self.placeholderLabel?.removeFromSuperview()
self.placeholderLabel = nil
super.removeFromSuperview()
}
override func placeholderRectForBounds(bounds: CGRect) -> CGRect {
let rect = super.placeholderRectForBounds(bounds)
if placeholderLabel == nil {
let label = UILabel(frame: rect)
label.textColor = UIColor(white: 0.7, alpha: 1.0)
label.font = self.font
placeholderLabel = label
addSubview(label)
}
setHighlightText(placeholderLabel!, state: self._highlightState)
layoutPlaceholderLabel(rect,!isFirstResponder())
return CGRect.zero
}
//布局提示文本
func layoutPlaceholderLabel(rect: CGRect,_ left: Bool = false) {
guard let label = placeholderLabel else {
return
}
let size = label.sizeThatFits(rect.size)
let frame = left ? rect : CGRect(x: super.clearButtonRectForBounds(bounds).minX - size.width, y: rect.minY, width: size.width, height: rect.height)
UIView.animateWithDuration(NSTimeInterval(animateDuration), delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
label.frame = frame;
}, completion: nil)
}
private func setHighlightText(label:UILabel, state:UITextBoxHighlightState) {
switch state {
case .Wrong(let errorText):
label.textColor = getTextColorWithHighlightColor(wrongColor)
label.text = errorText
case .Warning(let warningText):
label.textColor = getTextColorWithHighlightColor(warningColor)
label.text = warningText
case .Validator(let validatorText):
label.textColor = getTextColorWithHighlightColor(validatorColor)
label.text = validatorText
default:
if let attributedPlaceholder = self.attributedPlaceholder {
label.attributedText = attributedPlaceholder
} else {
label.text = self.placeholder
}
label.textColor = getTextColorWithHighlightColor(getHighlightColor(_highlightState))
}
}
private func getTextColorWithHighlightColor(color:UIColor) -> UIColor {
var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
color.getRed(&r, green: &g, blue: &b, alpha: &a)
return UIColor(red: r*r*0.7, green: g*g*0.7, blue: b*b*0.7, alpha: a) // 同类颜色加深一些
}
private func getHighlightColor(state:UITextBoxHighlightState) -> UIColor {
switch state {
case .Wrong: return wrongColor
case .Warning: return warningColor
case .Validator: return validatorColor
default: return self.isFirstResponder() ? highlightColor : self.backgroundColor ?? UIColor.lightTextColor()
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect)
{
// Drawing code
}
*/
}
extension UIColor {
convenience init(number:UInt32) {
let b = CGFloat(number & 0xFF) / 255
let g = CGFloat((number >> 8) & 0xFF) / 255
let r = CGFloat((number >> 16) & 0xFF) / 255
let a = number > 0xFFFFFF ? CGFloat((number >> 24) & 0xFF) / 255 : 1.0
self.init(red: r, green: g, blue: b, alpha: a)
}
convenience init?(hex:String) {
let regular:NSRegularExpression
do {
regular = try NSRegularExpression(pattern: "(#?|0x)[0-9a-fA-F]{2,}", options: NSRegularExpressionOptions.CaseInsensitive)
} catch { return nil }
let length = hex.startIndex.distanceTo(hex.endIndex) //distance(hex.startIndex, hex.endIndex)
guard let result = regular.firstMatchInString(hex, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, length)) else {
print("error: hex isn't color hex value!")
return nil
}
let start = hex.startIndex.advancedBy(result.rangeAtIndex(1).length + result.rangeAtIndex(1).location) //advance(hex.startIndex, result.rangeAtIndex(1).length + result.rangeAtIndex(1).location)
let end = hex.startIndex.advancedBy(result.range.length + result.range.location) //advance(hex.startIndex, result.range.length + result.range.location)
let number = strtoul(hex[start..<end], nil, 16)
let b = CGFloat((number >> 0) & 0xFF) / 255
let g = CGFloat((number >> 8) & 0xFF) / 255
let r = CGFloat((number >> 16) & 0xFF) / 255
let a = start.distanceTo(end) > 6 ? CGFloat((number >> 24) & 0xFF) / 255 : 1 //distance(start, end) > 6 ? CGFloat((number >> 24) & 0xFF) / 255 : 1
self.init(red: r, green: g, blue: b, alpha: a)
}
}