如何使用Swift以编程方式粘贴?

web*_*ets 5 paste ios swift

我希望有一个用户可以按下的按钮,它会自动粘贴剪贴板中的任何文本UITextView.

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

它已在这里得到解答,但它在Objective-C中.

Ast*_*oCB 11

你应该能够将它们转换为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
}
Run Code Online (Sandbox Code Playgroud)

  • Swift 5 ```let pb: UIPasteboard = UIPasteboard.general``` (2认同)