Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

ios - How to paste image from pasteboard on UITextView?

I have the following code on a keyboard extensión

let pasteboard = UIPasteboard.generalPasteboard()
var image = UIImage(named: "myimage");
pasteboard.image = image;

This doesn't work on a UITextView I have on my container application, paste context menu never shows up. It works on other applications like "messages" but not on mine.

My code works if I try to paste text instead of an image using string property so I'm quite near.

I could need to set up my text view different but I don't know how. I've changed "Text" from "Plain" to "Attributed" but still not working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It doesn't work if implemented in UITextView subclass, but I tried it in the UIViewController containing the textView and it worked:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {

    if (action == @selector(paste:)) {
        return [UIPasteboard generalPasteboard].string != nil || [UIPasteboard generalPasteboard].image != nil;
        //if you want to do this for specific textView add && [yourTextView isFirstResponder] to if statement
    }

    return [super canPerformAction:action withSender:sender];

}

-(void)paste:(id)sender {
    //do your action here
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...