-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUITextView+Validation.swift
More file actions
50 lines (43 loc) · 1.6 KB
/
UITextView+Validation.swift
File metadata and controls
50 lines (43 loc) · 1.6 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
//
// Validator
// Copyright © 2025 Space Code. All rights reserved.
//
#if os(iOS)
import UIKit
extension UITextView: IUIValidatable {
/// The value of the text view to validate.
/// Returns an empty string if `text` is nil.
public var inputValue: String {
text ?? ""
}
/// The type of input for validation.
public typealias Input = String
/// Enables or disables automatic validation when the text changes.
///
/// - Parameter isEnabled: If true, adds an observer for text changes.
/// If false, removes the observer.
public func validateOnInputChange(isEnabled: Bool) {
if isEnabled {
NotificationCenter.default.addObserver(
self,
selector: #selector(textViewDidChangeNotification(_:)),
name: UITextView.textDidChangeNotification,
object: self
)
} else {
NotificationCenter.default.removeObserver(
self,
name: UITextView.textDidChangeNotification,
object: self
)
}
}
// MARK: Private
/// Called automatically when the text view changes via NotificationCenter.
@objc
private func textViewDidChangeNotification(_ notification: Notification) {
guard let textView = notification.object as? UITextView, textView === self else { return }
validate(rules: validationRules)
}
}
#endif