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

前端之家收集整理的这篇文章主要介绍了iOS:如何将HTML复制到剪切粘贴缓冲区?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有兴趣让我的用户将他们输入的文本复制到剪切和粘贴缓冲区中,但我想将其作为 HTML来实现.

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

谢谢.

解决方法

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

猜你在找的iOS相关文章