ios 如何在 iphone 中以编程方式执行复制/粘贴功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8045970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do copy/paste function programmatically in iphone?
提问by ICoder
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.
我知道 iOS 中有一个默认的复制/粘贴菜单控制器,但我想通过单击按钮来执行此功能。我认为有UIPasteboard
这个功能,但我不知道如何使用它。
回答by Shishir.bobby
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];
}
回答by Suragch
Swift
迅速
This is the Swift version of the accepted answer.
这是已接受答案的 Swift 版本。
Copy
复制
UIPasteboard.general.string = myTextView.text
Paste
粘贴
if let myString = UIPasteboard.general.string {
myTextView.insertText(myString)
}
回答by benhorgen
For developers using MonoTouch, here are the two lines I used to complete the task in C#.
对于使用 MonoTouch 的开发人员,这里是我用来在 C# 中完成任务的两行代码。
The answer iscavenger provided to this question served as the model for my answer (after I successfully implemented it in my project ;-)
为这个问题提供的答案 iscavenger 作为我的答案的模型(在我在我的项目中成功实现它之后;-)
UIPasteboard clipboard = UIPasteboard.General;
clipboard.String = "string being added to clipboard";
回答by Ben Packard
回答by Michael Dautermann
I suspect you can relatively easily do what you want, starting with the [UIPasteboard dataForPasteboardType:]
method.
我怀疑你可以相对轻松地做你想做的事,从[UIPasteboard dataForPasteboardType:]
方法开始。
There's Apple sample code you can look into at:
您可以查看 Apple 示例代码: