I've tried so many different things based on what I found online and still can't do this. There are similar questions that I have tried to piece together but it's still not working. Here is what I have so far:
class CustomUITextView: UITextView {
override func paste(sender: AnyObject?) {
let data: NSData? = UIPasteboard.generalPasteboard().dataForPasteboardType("public.png")
if data != nil {
let attributedString = self.attributedText.mutableCopy() as! NSMutableAttributedString
let textAttachment = NSTextAttachment()
textAttachment.image = UIImage(data: data!)
let attrStringWithImage = NSAttributedString(attachment: textAttachment)
attributedString.replaceCharactersInRange(self.selectedRange, withAttributedString: attrStringWithImage)
self.attributedText = attributedString
} else {
let pasteBoard: UIPasteboard = UIPasteboard.generalPasteboard()
let text = NSAttributedString(string: pasteBoard.string!)
let attributedString = self.attributedText.mutableCopy() as! NSMutableAttributedString
attributedString.replaceCharactersInRange(self.selectedRange, withAttributedString: text)
self.attributedText = attributedString
}
}
}
Also, my IBOutlet for the textView is in another file and is of this custom type I created:
@IBOutlet var textView: CustomUITextView!
I created a subclass of UITextView and assigned it to my textview. I then override the paste function to allow for images since I read that UITextField does not support image pasting by default. This is currently working for the text but not the image ---> if I do a long press gesture to show the Menu, the "Paste" button never shows up if there is an image in the PasteBoard; it only shows if there is text.
Ultimately, I want to paste PNG, JPEGS, and images from URLS in my textview. Someone please help !!