如何在 iPhone 中以编程方式执行复制/粘贴功能?

发布于 2024-12-14 05:49:36 字数 188 浏览 0 评论 0原文

我有一个文本视图,其中包含一些文本和该视图中的复制按钮,

当用户输入一些文本并按下复制按钮时,它需要复制该文本并将该文本粘贴到他想要的任何地方。

我知道 iOS 中有一个默认的复制/粘贴菜单控制器,但我想通过单击按钮来执行此功能。我认为有 UIPasteboard 可以实现此功能,但我不知道如何使用它。

I have a text-view with some text and a copy button in that view,

When the user enters some text and presses the copy button, it needs to copy that text and paste that text wherever he wants.

I know there is a default copy/paste menu-controller in iOS, but I want to do this functionality in a button click. I think there is UIPasteboard to do this functionality, but I don't know how to use it.

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

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

发布评论

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

评论(5

徒留西风 2024-12-21 05:49:36

要从按钮复制​​,请单击:

- (IBAction)copy {
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    [pb setString:[textView text]];
}

要从按钮粘贴,请单击:

- (IBAction)paste {
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    textView.text = [pb string];
}

To copy from a button click:

- (IBAction)copy {
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    [pb setString:[textView text]];
}

To paste from a button click:

- (IBAction)paste {
    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    textView.text = [pb string];
}
假情假意假温柔 2024-12-21 05:49:36

Swift

这是已接受答案的 Swift 版本。

复制

UIPasteboard.general.string = myTextView.text

粘贴

if let myString = UIPasteboard.general.string {
    myTextView.insertText(myString)
}

Swift

This is the Swift version of the accepted answer.

Copy

UIPasteboard.general.string = myTextView.text

Paste

if let myString = UIPasteboard.general.string {
    myTextView.insertText(myString)
}
能怎样 2024-12-21 05:49:36

对于使用 MonoTouch 的开发人员,以下是我在 C# 中用来完成任务的两行代码。

为这个问题提供的答案 iscavenger 作为我答案的模型(在我在我的项目中成功实现它之后;-)

UIPasteboard clipboard = UIPasteboard.General;
clipboard.String =  "string being added to clipboard";

For developers using MonoTouch, here are the two lines I used to complete the task in C#.

The answer iscavenger provided to this question served as the model for my answer (after I successfully implemented it in my project ;-)

UIPasteboard clipboard = UIPasteboard.General;
clipboard.String =  "string being added to clipboard";
寄与心 2024-12-21 05:49:36

不知道为什么我们不能简单地使用:

[theTextView paste:nil];

按照 UIResponder 文档

Not sure why we can't simply use:

[theTextView paste:nil];

as per UIResponder docs

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