blob: 6973edb1fc231138731874db6015efab3c77a33c (
plain)
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
|
//
// AccountTextField.swift
// MullvadVPN
//
// Created by pronebird on 20/03/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import UIKit
class AccountTextField: CustomTextField, UITextFieldDelegate {
private let input = AccountTokenInput()
var onReturnKey: ((AccountTextField) -> Bool)?
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
cornerRadius = 0
delegate = self
pasteDelegate = input
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow(_:)),
name: UIWindow.keyboardWillShowNotification,
object: nil
)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var autoformattingText: String {
set {
input.replace(with: newValue)
input.updateTextField(self)
}
get {
input.formattedString
}
}
var parsedToken: String {
return input.parsedString
}
var enableReturnKey: Bool = true {
didSet {
updateKeyboardReturnKey()
}
}
// MARK: - UITextFieldDelegate
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return input.textField(textField, shouldChangeCharactersIn: range, replacementString: string)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return onReturnKey?(self) ?? true
}
// MARK: - Notifications
@objc private func keyboardWillShow(_ notification: Notification) {
if self.isFirstResponder {
updateKeyboardReturnKey()
}
}
// MARK: - Keyboard
private func updateKeyboardReturnKey() {
setEnableKeyboardReturnKey(enableReturnKey)
}
private func setEnableKeyboardReturnKey(_ enableReturnKey: Bool) {
let selector = NSSelectorFromString("setReturnKeyEnabled:")
if let inputDelegate = self.inputDelegate as? NSObject, inputDelegate.responds(to: selector) {
inputDelegate.setValue(enableReturnKey, forKey: "returnKeyEnabled")
}
}
// MARK: - Accessibility
override var accessibilityValue: String? {
set {
super.accessibilityValue = newValue
}
get {
if self.text?.isEmpty ?? true {
return ""
} else {
return super.accessibilityValue
}
}
}
}
|