我有兴趣让我的用户复制他们输入到剪切和粘贴缓冲区中的文本,但我想以 HTML 的形式进行。
这样的事情有可能吗?还是我需要使用 MIME 格式? (我不知道。)
谢谢。
最佳答案
以下代码会将您的 HTML 从您的应用程序中取出并放入 Apple 的邮件应用程序中。该文档在这方面没有给你很大的帮助,所以部分是查看 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:如何将 HTML 复制到剪切粘贴缓冲区中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6566152/