如何以编程方式粘贴Swift?

我想要一个button,用户可以按下,将自动粘贴剪贴板中的任何文本到UITextView

我怎么能在Swift中做到这一点?

这里已经回答了,但是在Objective-C中。

你应该能够将它们转换成Swift:

 @IBAction func copy() { let pb: UIPasteboard = UIPasteboard.generalPasteboard(); pb.string = textView.text // Or another source of text } @IBAction func paste() { let pb: UIPasteboard = UIPasteboard.generalPasteboard(); textView.text /*(Or somewhere to put the text)*/ = pb.string } 

您可以在Swift中实现复制和粘贴方法,如下所示:

 // Function receives the text as argument for copying func copyText(textToCopy : NSString) { let pasteBoard = UIPasteboard.generalPasteboard(); pasteBoard.string = textToCopy; // Set your text here } // Function returns the copied string func pasteText() -> NSString { let pasteBoard = UIPasteboard.generalPasteboard(); println("Copied Text : \(pasteBoard.string)"); // It prints the copied text return pasteBoard.string!; }