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

前端 未结 4 731
春和景丽
春和景丽 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 = @"

    Paragraft
    Less than a word processor, more than plain text"; 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/

提交回复
热议问题