-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordTextfield
More file actions
51 lines (42 loc) · 1.41 KB
/
PasswordTextfield
File metadata and controls
51 lines (42 loc) · 1.41 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
//
// PasswordTextField.swift
//
// Created by Akash on 23/02/24.
//
import Foundation
import UIKit
class PasswordTextField: UITextField{
private let eyeButton = UIButton(type: .custom)
@IBInspectable var showPasswordButton: Bool = false {
didSet {
setupEyeButton()
}
}
override func awakeFromNib() {
super.awakeFromNib()
setupEyeButton()
}
override init(frame: CGRect) {
super.init(frame: frame)
setupEyeButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupEyeButton()
}
private func setupEyeButton() {
guard showPasswordButton else {
rightViewMode = .never
return
}
eyeButton.setImage(UIImage(systemName: "eye"), for: .normal)
eyeButton.tintColor = .systemGray
eyeButton.addTarget(self, action: #selector(togglePasswordVisibility), for: .touchUpInside)
rightView = eyeButton
rightViewMode = .always
}
@objc private func togglePasswordVisibility() {
isSecureTextEntry.toggle()
eyeButton.setImage(UIImage(systemName: isSecureTextEntry ? ImagesKey.eye : ImagesKey.eyeSlash), for: .normal)
}
}