iPhone 粘贴板的基本用法

发布于 2024-07-26 02:00:10 字数 421 浏览 6 评论 0原文

我正在尝试在 iPhone 粘贴板中放入一些纯文本。 以下代码似乎不起作用:

UIPasteboard *pboard = [UIPasteboard generalPasteboard];
NSString *value = @"test";
[pboard setValue: value forPasteboardType: @"public.plain-text"];

我猜问题出在 PasteBoard 类型参数中。 传递 @"public.plain-text" 没有任何反应。 传递 kUTTypePlainText 时,编译器会抱怨指针类型不兼容,但不会崩溃,也不会发生任何事情。 使用 kUTTypePlainText 似乎还需要与 MobileCoreServices 链接,文档中没有提到这一点。

I'm trying to put some plain text in the iPhone Pasteboard. The following code doesn't seem to work:

UIPasteboard *pboard = [UIPasteboard generalPasteboard];
NSString *value = @"test";
[pboard setValue: value forPasteboardType: @"public.plain-text"];

I'm guessing the problem is in the PasteBoard type argument. Passing @"public.plain-text" nothing happens. Passing kUTTypePlainText the compiler complains about incompatible pointer type, but doesn't crash, and nothing happens either. Using kUTTypePlainText also seems to require linking with MobileCoreServices, which is not mentioned in the docs.

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

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

发布评论

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

评论(3

丘比特射中我 2024-08-02 02:00:10

使用此标头获取 kUTTypeUTF8PlainText 的值;

#import <MobileCoreServices/UTCoreTypes.h>

您需要有可用的 MobileCoreServices 框架。

Use this header to get the value for kUTTypeUTF8PlainText;

#import <MobileCoreServices/UTCoreTypes.h>

You'll need to have the MobileCoreServices framework available.

情丝乱 2024-08-02 02:00:10

回应评论和我自己的问题:

  • 设置 pasteboard 字符串属性有效。
  • 如果我使用 kUTTypeUTF8PlainText 而不是 kUTTypePlainText 作为粘贴板类型,则使用 setValue:forPasteboardType: 也有效。

我没有注意到字符串属性,因为我直接进入了“获取和设置单个粘贴板项目”任务部分。

我测试的方法是单击文本字段,看看是否会出现粘贴弹出窗口。

我仍然不确定文档中的 UTT 类型在 iPhone 中的解释,包括从哪里获取它们(框架,#include 文件),似乎“统一类型标识符概述”文档是仍然面向Mac OS。 由于常量给了我一个类型不匹配警告,我认为我做错了什么,这就是我第一次尝试使用 NSString 文字的原因。

Responding to the comments and my own question:

  • Setting the pasteboard string property works.
  • Using setValue:forPasteboardType: also works if I use kUTTypeUTF8PlainText instead of kUTTypePlainText for the pasteboard type.

I had not noticed the string property because I went directly to the "Getting and Setting Single Pasteboard Items" tasks section.

The way I was testing was by clicking in a text field and see if the paste pop-up would appear.

I still am not sure where in the docs the UTT types are explained for the iPhone, including where to get them (Framework, #include files), it seems that the "Uniform Type Identifiers Overview" doc is still geared toward Mac OS. Since the constants gave me a type mismatch warning I thought I was doing something wrong, that's why I first tried using an NSString literal.

我还不会笑 2024-08-02 02:00:10

这是我将文本粘贴到粘贴板上的实验。 我正在使用按钮以编程方式添加文本。

#import <MobileCoreServices/MobileCoreServices.h>

- (IBAction)setPasteboardText:(id)sender
{
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    NSString *text = @"東京京都大阪";

    // Works, but generates an incompatible pointer warning
    [pb setValue:text forPasteboardType:kUTTypeText];

    // Puts generic item (not text type), can't be pasted into a text field
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeItem];

    // Works, even with non-ASCII text
    // I would say this is the best way to do it with unknown text
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeText];

    // Works without warning
    // This would be my preferred method with UTF-8 text
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeUTF8PlainText];

    // Works without warning, even with Japanese characters
    [pb setValue:text forPasteboardType:@"public.plain-text"];

    // Works without warning, even with Japanese characters
    [pb setValue:text forPasteboardType:@"public.text"];

    // Check contents and content type of pasteboard
    NSLog(@"%@", [pb items]);
}

我将内容粘贴到文本字段中进行检查,并每次更改文本内容以确保它不仅仅是重复使用以前的粘贴。

Here's my experiments with pasting text onto the pasteboard. I'm using a button to add the text programatically.

#import <MobileCoreServices/MobileCoreServices.h>

- (IBAction)setPasteboardText:(id)sender
{
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    NSString *text = @"東京京都大阪";

    // Works, but generates an incompatible pointer warning
    [pb setValue:text forPasteboardType:kUTTypeText];

    // Puts generic item (not text type), can't be pasted into a text field
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeItem];

    // Works, even with non-ASCII text
    // I would say this is the best way to do it with unknown text
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeText];

    // Works without warning
    // This would be my preferred method with UTF-8 text
    [pb setValue:text forPasteboardType:(NSString *)kUTTypeUTF8PlainText];

    // Works without warning, even with Japanese characters
    [pb setValue:text forPasteboardType:@"public.plain-text"];

    // Works without warning, even with Japanese characters
    [pb setValue:text forPasteboardType:@"public.text"];

    // Check contents and content type of pasteboard
    NSLog(@"%@", [pb items]);
}

I pasted the contents into a text field to check, and changed the text contents each time to make sure it wasn't just re-using the previous paste.

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