iOS: How to copy HTML into the cut-paste buffer?

前端 未结 4 719
春和景丽
春和景丽 2020-12-08 18:09

I\'m interested in letting my users copy the text they\'ve entered into the cut-and-paste buffer, but I\'d like to do that as HTML.

Is such a thing even possible? O

相关标签:
4条回答
  • 2020-12-08 18:25

    The following code will get your HTML out of your app and into Apple's Mail app. The documentation doesn't give you a great deal of help on this, so in part it's a matter of looking at what Apple's apps park on the pasteboard and then reverse engineering that. This solution draws on an earlier stackoverflow post - follow up the links there for more background.

    NSLog(@"Place HTML on the pasteboard");
    
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    NSString *htmlType = @"Apple Web Archive pasteboard type";
    
    // example html string
    NSString* htmlString = @"<p style=\"color:gray\"> <a href=@\"http://itunes.apple.com/gb/app/paragraft/id412998778?mt=8\">Paragraft</a><br><em>Less than a word processor, more than plain text</em>";
    
    NSMutableDictionary *resourceDictionary = [NSMutableDictionary dictionary];    
    
    [resourceDictionary setObject:[htmlString dataUsingEncoding:NSUTF8StringEncoding]  forKey:@"WebResourceData"];
    
    [resourceDictionary setObject:@"" forKey:@"WebResourceFrameName"];
    [resourceDictionary setObject:@"text/html" forKey:@"WebResourceMIMEType"];
    [resourceDictionary setObject:@"UTF-8" forKey:@"WebResourceTextEncodingName"];
    [resourceDictionary setObject:@"about:blank" forKey:@"WebResourceURL"];
    
    NSDictionary *containerDictionary = [NSDictionary dictionaryWithObjectsAndKeys:resourceDictionary, @"WebMainResource", nil];
    
    NSDictionary *htmlItem = [NSDictionary dictionaryWithObjectsAndKeys:containerDictionary,htmlType,nil];
    
    [pasteboard setItems: [NSArray arrayWithObjects: htmlItem, nil]];
    
    // This approach draws on the blog post and comments at:
    // http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/
    
    0 讨论(0)
  • 2020-12-08 18:30

    This solution puts both a HTML and a plain text representation into the pasteboard:

    #import <MobileCoreServices/MobileCoreServices.h>
    
    NSString *html = @"<h1>Headline</h1>text";
    NSData *data =  [html dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict = @{@"WebMainResource": @{@"WebResourceData": data, @"WebResourceFrameName": @"", @"WebResourceMIMEType": @"text/html", @"WebResourceTextEncodingName": @"UTF-8", @"WebResourceURL": @"about:blank"}};
    data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
    NSString *archive = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString *plain = [html stringByReplacingOccurrencesOfRegex:@"<[^>]+>" withString:@""];
    [UIPasteboard generalPasteboard].items = @[@{@"Apple Web Archive pasteboard type": archive, (id)kUTTypeUTF8PlainText: plain}];
    

    It uses -stringByReplacingOccurrencesOfRegex: from RegexKitLite to strip the HTML tags.

    0 讨论(0)
  • 2020-12-08 18:30

    I use w3schools. I cut and paste my html code over their example code , on any of their many "Try it yourself" tutorials and then use their "run" button.

    e.g. https://www.w3schools.com/html/default.asp

    0 讨论(0)
  • 2020-12-08 18:31

    I absolutely adore this method of creating HTML-based content that you can paste into other HTML-aware apps, like Mail. However, I noticed that the above solution by Matthew Elton only allowed the pasteboard to be pasted onto HTML-aware apps. Trying to paste the exact same content into the Notes app for example, would fail.

    I took the tips from this post: https://stackoverflow.com/a/1078471/351810 and can now successfully paste both HTML and plain text versions of the content that I want.

    0 讨论(0)
提交回复
热议问题