-
Notifications
You must be signed in to change notification settings - Fork 184
/
RAMTextField.swift
113 lines (85 loc) · 2.94 KB
/
RAMTextField.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
//
// RAMTextField.swift
// RAMReel
//
// Created by Mikhail Stepkin on 4/22/15.
// Copyright (c) 2015 Ramotion. All rights reserved.
//
import UIKit
// MARK: - RAMTextField
/**
RAMTextField
--
Textfield with a line in the bottom
*/
open class RAMTextField: UITextField {
/**
Overriding UIView's drawRect method to add line in the bottom of the Text Field
- parameter rect: Rect that should be updated. This override ignores this parameter and redraws all text field
*/
override open func draw(_ rect: CGRect) {
let rect = self.bounds
let ctx = UIGraphicsGetCurrentContext()
let lineColor = self.tintColor.withAlphaComponent(0.3)
lineColor.set()
ctx?.setLineWidth(1)
let path = CGMutablePath()
// var m = CGAffineTransform.identity
// CGPathMoveToPoint(path, &m, 0, rect.height)
path.move(to: CGPoint(x: 0, y: rect.height))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
// CGPathAddLineToPoint(path, &m, rect.width, rect.height)
ctx?.addPath(path)
ctx?.strokePath()
}
}
// MARK: - UITextField extensions
extension UITextField {
/**
Overriding `UITextField` `tintColor` property to make it affect close image tint color.
*/
open override var tintColor: UIColor! {
get {
return super.tintColor
}
set {
super.tintColor = newValue
let subviews = self.subviews
for view in subviews {
guard let button = view as? UIButton else {
break
}
let states: [UIControl.State] = [.highlighted]
states.forEach { state -> Void in
let image = button.image(for: state)?.tintedImage(self.tintColor)
button.setImage(image, for: state)
}
}
}
}
}
// MARK: - UIImage extensions
private extension UIImage {
/**
Create new image by applying a tint.
- parameter color: New image tint color.
*/
func tintedImage(_ color: UIColor) -> UIImage {
let size = self.size
UIGraphicsBeginImageContextWithOptions(size, false, self.scale)
let context = UIGraphicsGetCurrentContext()
self.draw(at: CGPoint.zero, blendMode: CGBlendMode.normal, alpha: 1.0)
context?.setFillColor(color.cgColor)
context?.setBlendMode(CGBlendMode.sourceIn)
context?.setAlpha(1.0)
let rect = CGRect(
x: CGPoint.zero.x,
y: CGPoint.zero.y,
width: size.width,
height: size.height)
UIGraphicsGetCurrentContext()?.fill(rect)
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return tintedImage ?? self
}
}