iOS:如何将HTML复制到剪贴板?

7

我希望让我的用户将他们输入的文本复制到剪贴板中,但我希望以HTML格式进行。这种事情是否可能?还是我需要使用MIME格式?(我不知道。)

谢谢。

4个回答

7
以下代码将使你的HTML从应用程序中导出到苹果邮件应用程序。文档对此并没有给予太多帮助,因此在某种程度上,这是通过查看苹果应用程序在粘贴板上停留的内容,然后进行反向工程处理的问题。该解决方案基于早期的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/

4
这个解决方案将HTML格式和纯文本格式都放入了剪贴板中:
#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}];

它使用来自RegexKitLite-stringByReplacingOccurrencesOfRegex:来去除HTML标签。


在某种程度上它是有效的,但是它没有给我想要的结果。http://tinypic.com/view.php?pic=2yw8tqq&s=8#.UvK-rPYkgnU 不知道为什么它没有消除HTML标记。 - jamil
这是我的代码 http://pastebin.com/Rg4QWUEK 任何建议都将不胜感激。谢谢。 - jamil
当您在调试器中逐步执行时,plain 在方法结束时是否具有正确的纯文本表示形式? - Ortwin Gentz
当我使用 NSLog(@"Plain Text %@",plain); 时,它会显示相同的结果 <h2>Word</h2> اب تک<h2>Meaning</h2> 迄今为止/至今为止。当我通过邮件发送时,同样的事情运行良好。 - jamil
1
@OrtwinGentz - 您的解决方案仍然非常有效。我只更新了一行代码,原来使用的是 'dataFromPropertyList',现在改为:data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];因为在2018年,该函数已被弃用。 - Mayank Kumar
显示剩余2条评论

0

我非常喜欢这种创建基于HTML的内容并将其粘贴到其他HTML感知应用程序(如Mail)中的方法。然而,我注意到Matthew Elton提供的解决方案只允许将剪贴板粘贴到HTML感知应用程序中。例如,尝试将完全相同的内容粘贴到Notes应用程序中会失败。

我从这篇文章中获得了一些技巧:https://dev59.com/kHNA5IYBdhLWcg3wQ7Yw#1078471,现在我可以成功地粘贴所需内容的HTML和纯文本版本。


这在iOS 4上运行得非常完美,但当我尝试在iOS 5上运行时,HTML似乎有点损坏。我可以成功地粘贴<a href>\链接,但是像加粗和颜色这样的格式好像被忽略了。 - XCool

-1

我使用w3schools。 我将我的HTML代码剪切并粘贴到他们的示例代码上,在他们众多的“尝试自己”教程中,然后使用他们的“运行”按钮。

例如:https://www.w3schools.com/html/default.asp


糟糕,我使用任何浏览器的“查看页面源代码”来获取HTML。但那是在使用Windows系统。希望能对其他人有所帮助。 - user12983814

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接