iOS:如何将HTML复制到剪切粘贴缓冲区?

Cod*_*key 6 clipboard cocoa-touch uikit uipasteboard ios

我有兴趣让我的用户将他们输入的文本复制到剪切和粘贴缓冲区中,但我想将其作为HTML来实现.

这样的事情甚至可能吗?或者我需要使用MIME格式?(我不知道.)

谢谢.

Obl*_*ely 6

以下代码将从您的应用程序和Apple的Mail应用程序中获取HTML.该文档并没有为您提供大量帮助,因此部分原因在于查看Apple的应用程序驻留在粘贴板上然后进行逆向工程.该解决方案利用了早期的stackoverflow帖子 - 跟进那里的链接以获得更多背景.

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/
Run Code Online (Sandbox Code Playgroud)