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

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

$
0
0

textView(_, shouldChangeTextIn, replacementText) can be called in many cases. if you will read UIPasteboard.general.string value every time, user will see system alert that application accessed pasteboard after every letter typed.

To avoid that problem you can create subclass of UITextView and override paste(_) function in it. This function is being called by system once every time when user tries to paste something.

Use next call of textView(_, shouldChangeTextIn, replacementText) to handle paste event.

var isPastingContent = false // helper variableopen override func paste(_ sender: Any?) {    isPastingContent = true // next call of `shouldChangeTextIn` will be for the paste action    super.paste(sender)}func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {    if isPastingContent { // if we detected paste event         // paste detected, do what you want here         let pasteboardContent = UIPasteboard.general.string // you can get pasteboard content here safely. System alert will be shown only once.         isPastingContent = false // toggle helper value back to false    }}

Viewing all articles
Browse latest Browse all 14

Trending Articles



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