如何用Swift将文本复制到剪贴板/粘贴板上

时间:2022-11-09 10:44:09

I'm looking for a clean example of how to copy text to iOS clipboard that can then be used/pasted in other apps.

我正在寻找一个清晰的例子,说明如何将文本复制到iOS剪贴板,然后在其他应用程序中使用/粘贴。

The benefit of this function is that the text can be copied quickly, without the standard text highlighting functions of the traditional text copying.

该函数的好处是可以快速复制文本,而不需要传统文本复制的标准文本高亮显示功能。

I am assuming that the key classes are in UIPasteboard, but can't find the relevant areas in the code example they supply: https://developer.apple.com/documentation/uikit/uipasteboard

我假设关键类在UIPasteboard中,但是在它们提供的代码示例中找不到相关区域:https://developer.apple.com/documentation/uikit/uipasteboard

3 个解决方案

#1


241  

If all you want is plain text, you can just use the string property:

如果你只想要纯文本,你可以使用string属性:

UIPasteboard.generalPasteboard().string = "Hello world"

In Swift 3:

在斯威夫特3:

UIPasteboard.general.string = "Hello world"

#2


23  

Since copying and pasting is usually done in pairs, this is supplemental answer to @jtbandes good, concise answer. I originally came here looking how to paste.

由于复制和粘贴通常是成对进行的,这是对@jtbandes的补充回答,好的,简洁的回答。我本来是来这里看看怎么粘贴的。

iOS makes this easy because the general pasteboard can be used like a variable. Just get and set UIPasteboard.general.string.

iOS使这变得容易,因为一般的pasteboard可以用作变量。获取并设置UIPasteboard.general.string。

Here is an example showing both being used with a UITextField:

这里有一个例子显示两者都被UITextField使用:

Copy

复制

UIPasteboard.general.string = myTextField.text

Paste

粘贴

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

Note that the pasteboard string is optional, so it has to be unwrapped first.

请注意,pasteboard字符串是可选的,因此必须先打开它。

#3


3  

For Swift 3 Copy from app to clipboard

Swift 3从app拷贝到剪贴板

   let pasteboard = UIPasteboard.general
    pasteboard.string = employee.phone_number

#1


241  

If all you want is plain text, you can just use the string property:

如果你只想要纯文本,你可以使用string属性:

UIPasteboard.generalPasteboard().string = "Hello world"

In Swift 3:

在斯威夫特3:

UIPasteboard.general.string = "Hello world"

#2


23  

Since copying and pasting is usually done in pairs, this is supplemental answer to @jtbandes good, concise answer. I originally came here looking how to paste.

由于复制和粘贴通常是成对进行的,这是对@jtbandes的补充回答,好的,简洁的回答。我本来是来这里看看怎么粘贴的。

iOS makes this easy because the general pasteboard can be used like a variable. Just get and set UIPasteboard.general.string.

iOS使这变得容易,因为一般的pasteboard可以用作变量。获取并设置UIPasteboard.general.string。

Here is an example showing both being used with a UITextField:

这里有一个例子显示两者都被UITextField使用:

Copy

复制

UIPasteboard.general.string = myTextField.text

Paste

粘贴

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

Note that the pasteboard string is optional, so it has to be unwrapped first.

请注意,pasteboard字符串是可选的,因此必须先打开它。

#3


3  

For Swift 3 Copy from app to clipboard

Swift 3从app拷贝到剪贴板

   let pasteboard = UIPasteboard.general
    pasteboard.string = employee.phone_number