将 HTML 复制到 UIPasteboard

发布于 2024-08-24 20:00:33 字数 418 浏览 5 评论 0原文

我想将 HTML 从我的应用程序传输到 iPhone 邮件应用程序。 我已经有了 HTML 文本 - 以免说 Test 我可以将其放置到 UIPasteBoard - 但是当我将其粘贴到邮件时,我得到了 html 源代码。

当我将相同的字符串放入 HTMLView 中时 - 选择它并复制它,它将作为红色文本粘贴到邮件中。

我需要做什么才能将字符串放入 UIPasteBoard 中,以便将其作为红色文本粘贴到邮件应用程序中? 我一直在搜索“格式类型” - 并发现当我在剪贴板中有元素(从 UIWebView 复制)时,UIPasteBoard 返回“Aplle Web Archive 粘贴板类型”。 但是,在将内容添加到 UIPasteBoard 时将此设置为类型不会在邮件应用程序中粘贴任何内容。

曼弗雷德

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

触ぅ动初心 2024-08-31 20:00:33

那不是真的。您可以将任何内容粘贴到粘贴板上,去阅读文档。

我终于整理了一个教程,展示如何将 HTML 复制到邮件应用程序中。
http://mcmurrym.wordpress。 com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/

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/

小嗷兮 2024-08-31 20:00:33

我已经使 HTML 复制正常工作,因此它可以正确粘贴到内置的邮件和便笺应用程序中。它看起来像这样:

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]];

要访问这些类型常量,您需要导入:

#import <MobileCoreServices/UTCoreTypes.h>

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>
孤独难免 2024-08-31 20:00:33

在您在评论中提供的同一链接上,您会在顶部找到此段落。

统一类型标识符 (UTI) 经常用于表示类型(有时称为粘贴板类型)。 例如,您可以使用 kUTTypeJPEG(public.jpeg 的常量)作为 JPEG 数据的表示类型。但是,应用程序可以自由地使用任何它们想要的字符串作为表示类型;但是,对于特定于应用程序的数据类型,建议您使用反向 DNS 表示法以确保类型的唯一性(例如 com.myCompany.myApp.myType)。

其正下方是此处的链接。 http://developer.apple.com/iphone/library/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319

其中解释了UTI。

最后,此链接为您提供了几种类型 http://developer.apple.com/iphone/library/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259 -SW1

当然,该列表并不是所有类型,因为您可以创建自己的类型。

我已成功将 html 粘贴到邮件应用程序中。我将为您提供一个很好的起点...

创建一个将在粘贴板中显示数据类型的应用程序。转到设备上的 safari,复制网页。运行您的应用程序,您会注意到粘贴板类型是“Apple Web Archive 粘贴板类型”。请注意,这实际上是一种粘贴板类型(自定义类型)。如果您尝试通过创建 Web 存档来自己复制 Safari 移动复制和粘贴功能,并尝试将其作为文本粘贴到邮件应用程序中,则会将 Web 存档文件显示为原始 xml。如果您将类型定义为“Apple Web Archive 粘贴板类型”,则邮件应用程序实际上会将粘贴格式设置为 html。

如果您想知道网络档案是什么样子。在桌面 safari 上,只需将网页另存为存档并在文本阅读器中查看该文件(文本编辑将尝试解析它,因此您可以使用不同的程序来查看存档 xml)。

请阅读所有文档,因为您会发现您可以在发送给我的链接中执行自定义类型。

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.

謌踐踏愛綪 2024-08-31 20:00:33

这是绝对有可能的。 UIPasteboard 通常包含多个值,按其 UTI 索引。

以下是如何将 public.html 作为主要类型的工作示例。

-(NSString * _Nullable) getHtmlFromPB {
    static NSArray * prefs = @[@"public.html",@"public.utf8-plain-text",@"public.plain-text"];
    if (!UIPasteboard.generalPasteboard.hasStrings) return nil;
    for (NSString * theone in prefs) {
        if ([[UIPasteboard generalPasteboard].pasteboardTypes indexOfObject:theone] == NSNotFound) continue;
        return [UIPasteboard.generalPasteboard.items.firstObject objectForKey:theone];
    }
    return nil;
}

如果您需要解释:prefs 数组包含按所需顺序粘贴板内容的首选 UTI。因此,要么需要其中之一,要么什么都不用。

It is absolutely possible. UIPasteboard usually contains multiple values, indexed by their UTI.

Here is a working example of how to get public.html as a primary type.

-(NSString * _Nullable) getHtmlFromPB {
    static NSArray * prefs = @[@"public.html",@"public.utf8-plain-text",@"public.plain-text"];
    if (!UIPasteboard.generalPasteboard.hasStrings) return nil;
    for (NSString * theone in prefs) {
        if ([[UIPasteboard generalPasteboard].pasteboardTypes indexOfObject:theone] == NSNotFound) continue;
        return [UIPasteboard.generalPasteboard.items.firstObject objectForKey:theone];
    }
    return nil;
}

If you need explaination: prefs array contains preferred UTI for Pasteboard content in desired order. So, it will take one of them or nothing.

生死何惧 2024-08-31 20:00:33

不,不能。 UIPasterBoard 仅接受字符串、图像、URL 和颜色。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文