-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesignableTextField.swift
85 lines (64 loc) · 1.97 KB
/
DesignableTextField.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
//
// DesignableTextField.swift
// Created by Umair on 26/02/2020.
// Copyright © 2020 Umair . All rights reserved.
import Foundation
import UIKit
class DesignableTextField: UITextField {
@IBInspectable var rightImage : UIImage? {
didSet {
updateRightView()
}
}
@IBInspectable var rightPadding : CGFloat = 0 {
didSet {
updateRightView()
}
}
func updateRightView() {
if let image = rightImage {
rightViewMode = .always
// assigning image
let imageView = UIImageView(frame: CGRect(x: rightPadding, y: 0, width: 20, height: 20))
imageView.image = image
var width = rightPadding - 20
if borderStyle == UITextField.BorderStyle.none || borderStyle == UITextField.BorderStyle.line {
width -= 5
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 20)) // has 5 point higher in width in imageView
view.addSubview(imageView)
rightView = view
} else {
// image is nill
rightViewMode = .never
}
}
@IBInspectable var leftImage : UIImage? {
didSet {
updateLeftView()
}
}
@IBInspectable var leftPadding : CGFloat = 0 {
didSet {
updateLeftView()
}
}
func updateLeftView() {
if let image = leftImage {
leftViewMode = .always
// assigning image
let imageView = UIImageView(frame: CGRect(x: leftPadding, y: 0, width: 20, height: 20))
imageView.image = image
var width = leftPadding + 20
if borderStyle == UITextField.BorderStyle.none || borderStyle == UITextField.BorderStyle.line {
width += 5
}
let view = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 20)) // has 5 point higher in width in imageView
view.addSubview(imageView)
leftView = view
} else {
// image is nill
leftViewMode = .never
}
}
}