summaryrefslogtreecommitdiffhomepage
path: root/ios
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2021-04-12 14:28:22 +0200
committerAndrej Mihajlov <and@mullvad.net>2021-04-12 14:28:22 +0200
commit80483c8885082d7c6b90740f766c10b2cb245bda (patch)
tree26ab297003bb1d607fe3ded93cc6cbe5517f49c5 /ios
parentf710fac616c0576a474af325159d1055c77568e3 (diff)
parent5f5fe4c01ca0285c694921aac6a85c80cc9adead (diff)
downloadmullvadvpn-80483c8885082d7c6b90740f766c10b2cb245bda.tar.xz
mullvadvpn-80483c8885082d7c6b90740f766c10b2cb245bda.zip
Merge branch 'add-return-key-handler'
Diffstat (limited to 'ios')
-rw-r--r--ios/MullvadVPN/AccountInputGroupView.swift2
-rw-r--r--ios/MullvadVPN/AccountTextField.swift48
2 files changed, 48 insertions, 2 deletions
diff --git a/ios/MullvadVPN/AccountInputGroupView.swift b/ios/MullvadVPN/AccountInputGroupView.swift
index f3c2a05d8d..5f9c99190f 100644
--- a/ios/MullvadVPN/AccountInputGroupView.swift
+++ b/ios/MullvadVPN/AccountInputGroupView.swift
@@ -26,6 +26,8 @@ class AccountInputGroupView: UIView {
textField.smartQuotesType = .no
textField.spellCheckingType = .no
textField.keyboardType = .numberPad
+ textField.returnKeyType = .done
+ textField.enablesReturnKeyAutomatically = false
return textField
}()
diff --git a/ios/MullvadVPN/AccountTextField.swift b/ios/MullvadVPN/AccountTextField.swift
index 0e51e3f6b2..a7ddbb7a84 100644
--- a/ios/MullvadVPN/AccountTextField.swift
+++ b/ios/MullvadVPN/AccountTextField.swift
@@ -8,10 +8,12 @@
import UIKit
-@IBDesignable class AccountTextField: UITextField {
+class AccountTextField: UITextField, UITextFieldDelegate {
private let input = AccountTokenInput()
+ var onReturnKey: ((AccountTextField) -> Bool)?
+
override init(frame: CGRect) {
super.init(frame: frame)
setup()
@@ -25,8 +27,15 @@ import UIKit
private func setup() {
backgroundColor = UIColor.clear
- delegate = input
+ delegate = self
pasteDelegate = input
+
+ NotificationCenter.default.addObserver(
+ self,
+ selector: #selector(keyboardWillShow(_:)),
+ name: UIWindow.keyboardWillShowNotification,
+ object: nil
+ )
}
var autoformattingText: String {
@@ -43,6 +52,12 @@ import UIKit
return input.parsedString
}
+ var enableReturnKey: Bool = true {
+ didSet {
+ updateKeyboardReturnKey()
+ }
+ }
+
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 14, dy: 12)
}
@@ -51,4 +66,33 @@ import UIKit
return textRect(forBounds: bounds)
}
+ // 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: - Keyboard notifications
+
+ @objc private func keyboardWillShow(_ notification: Notification) {
+ if self.isFirstResponder {
+ updateKeyboardReturnKey()
+ }
+ }
+
+ 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")
+ }
+ }
+
}