7

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? Or do I need to use a MIME format? (I have no idea.)

Thanks.

Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
Code Monkey
  • 71
  • 1
  • 2

4 Answers4

7

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/
Community
  • 1
  • 1
Obliquely
  • 7,002
  • 2
  • 32
  • 51
4

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.

Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
  • To some extent its working but it is giving me the desired result.http://tinypic.com/view.php?pic=2yw8tqq&s=8#.UvK-rPYkgnU don't know why it did not elimenting the html tag.? – jamil Feb 05 '14 at 22:46
  • here is my code http://pastebin.com/Rg4QWUEK any suggestion will be appriated.Thanks – jamil Feb 05 '14 at 22:49
  • When you step through in the debugger, does `plain` have the correct text-only representation at the end of the method? – Ortwin Gentz Feb 06 '14 at 08:51
  • when i NSLog(@"Plain Text %@",plain); it show me same result

    Word

    اب تک

    Meaning

    heretofore/ hitherto the same thing is working fine when i sending in mail.
    – jamil Feb 06 '14 at 11:51
  • Then the Regex to strip the HTML tags isn't working. Maybe some encoding issue when copying the code? To validate, set `plain` to some plain text. It should be pasted into plaintext fields such as a `UISearchBar`. – Ortwin Gentz Feb 06 '14 at 13:30
  • 1
    @OrtwinGentz - Your solution still works great. I just have updated one line that was using 'dataFromPropertyList ' to data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil]; Because in 2018 this function has been deprecated. – Mayank Kumar Jan 30 '18 at 10:03
  • Thanks, @MayankKumar, fixed. – Ortwin Gentz Jan 30 '18 at 16:10
0

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.

Community
  • 1
  • 1
XCool
  • 976
  • 11
  • 32
-1

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

  • Whoops, I use "view page source" from any brower to get the html. But that is using Windows. Hope it helps someone else. – user12983814 Feb 29 '20 at 10:30