Quantcast
Channel: How to know when text is pasted into a UITextView? - Stack Overflow
Viewing all articles
Browse latest Browse all 14

Answer by hstdt for How to know when text is pasted into a UITextView?

$
0
0

iOS 13 with Combine

public extension UITextView {    var textDidChangePublisher: AnyPublisher<String, Never> {        NotificationCenter.default            .publisher(for: UITextView.textDidChangeNotification, object: self)            .compactMap { $0.object as? UITextView }            .compactMap(\.text)            .eraseToAnyPublisher()    }    var attributedTextDidChangePublisher: AnyPublisher<NSAttributedString, Never> {        NotificationCenter.default            .publisher(for: UITextView.textDidChangeNotification, object: self)            .compactMap { $0.object as? UITextView }            .compactMap(\.attributedText)            .eraseToAnyPublisher()    }}
var cancellable = Set<AnyCancellable>()textView.textDidChangePublisher    .removeDuplicates()    .sink { [weak self] newValue in        guard let self = self else { return }        // what you want    }    .store(in: &cancellable)

Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>