1

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 !!

JEL
  • 1,540
  • 4
  • 23
  • 51
  • So where do you modify UIMenuController? I don't see that in your code. If you don't do that, the Paste button obviously won't show up. – matt Sep 12 '15 at 01:16
  • @matt I have not modified UIMenuController anywhere in my code. I thought I didn't need to since it still appears and also works for copying and pasting "text". How would I modify it to work with images? – JEL Sep 12 '15 at 01:21
  • So did you actually look at e.g. this question and answer? http://stackoverflow.com/questions/29175089/how-to-paste-image-from-pasteboard-on-uitextview – matt Sep 12 '15 at 01:29
  • @matt Thanks for sending link! Yes I have tried that. I was able to paste images to my textView only if it was already in Xcode. I couldn't get it to work if I copied an image from Apple Notes app or Google Images. Ultimately, I ended up with what I have now thanks to this link: http://stackoverflow.com/questions/13263808/adding-images-to-uitextview – JEL Sep 12 '15 at 01:33

0 Answers0