copy HTML to UIPasteboard

2020-02-29 07:09发布

问题:

I want to transfer HTML from my app to the iPhone mail application. I already have the HTML text - lest say <span style='color:red'>Test</span> I can place this to UIPasteBoard - but when I paste it to mail I get the html source.

When I place the same string in a HTMLView - select it there and copy it it pastes as red text in mail.

What do I have to do to place the string in the UIPasteBoard so that it pastes as red text to the mail application? I've been searching for "format types" - and found that UIPasteBoard returns "Aplle Web Archive pasteboard type" when I have the element (copied from UIWebView) in the clipboard. But setting this as type when adding the content to UIPasteBoard pastes nothing in the mail app.

Manfred

回答1:

No, it cannot. UIPasterBoard only accepts strings, images, URLs and colors.



回答2:

That is not true. you can paste ANYTHING to the pasteboard, go read the docs.

I've finally put together a tutorial that shows how to copy HTML into the Mail app. http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/



回答3:

I've got HTML copy working so it pastes into the built-in Mail and Notes apps properly. It looks like this:

NSString *htmlContent = @"This is <span style='font-weight:bold'>HTML</span>";
NSString *content = @"This is HTML!";
NSDictionary *dict = @{(NSString *)kUTTypeText: content, (NSString *)kUTTypeHTML: htmlContent};
[[UIPasteboard generalPasteboard] setItems:@[dict]];

To get access to those type constants, you need to import this:

#import <MobileCoreServices/UTCoreTypes.h>


回答4:

on that same link you gave in your comment, you'll find this paragraph at the top.

A Uniform Type Identifier (UTI) is frequently used for a representation type (sometimes called a pasteboard type). For example, you could use kUTTypeJPEG (a constant for public.jpeg) as a representation type for JPEG data. However, applications are free to use any string they want for a representation type; however, for application-specific data types, it is recommended that you use reverse-DNS notation to ensure the uniqueness of the type (for example, com.myCompany.myApp.myType).

Right below it is a link to here. http://developer.apple.com/iphone/library/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319

Which explains UTIs.

Finally this link gives you SEVERAL types http://developer.apple.com/iphone/library/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

Of course that list isn't ALL types as you can create your own types.

I have successfully pasted html into the mail app. I'll give you a good place to start...

Create an app that will show the data types in the pasteboard. Goto safari on the device, copy a web page. Run your app, You'll notice the pasteboard type is "Apple Web Archive pasteboard type." Note that this is really a pasteboard type (a custom one). If you try to duplicate the safari mobile copy and paste feature yourself by creating a web archive and attempt to paste it as text into the mail app, it will show the web archive file as raw xml. If you define the type as "Apple Web Archive pasteboard type" the mail app will actually format the paste as html.

If you want to know what a web archive looks like. On desktop safari, just save a web page as an archive and look at the file in a text reader (text edit will try to parse it, so you might use a different program to look at the archive xml).

Please read all of the documentation as you can discover that you can do custom types in the link that you sent me.