特定于应用程序的 UIPasteboard

app-specific UIPasteboard

我正在尝试创建特定于应用程序的 UIPasteboard,但在弄清楚如何注册它时遇到了一些问题:

UIPasteboard *pasteboard = [UIPasteboard pasteboardWithName:@"myPasteboard" create:YES];

如果我只是创建它,应用程序会继续使用 generalPasteboard。我真的希望我不必捕获 cut/copy/paste 操作并手动设置项目。

//Copy your text field text and Just App in Pasteboard
NSString * text=textfield.text;
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
[pasteboard setString:text];

下面是关于粘贴板的教程。 http://hayageek.com/uipasteboard-example-read-write-share/

请阅读本文 http://nshipster.com/uimenucontroller/(它是关于 UILabel,而不是 UITextField,但仅用于示例和有关 copy/paste/edit 操作如何工作以及如何自定义其行为的信息)

例如,我认为您需要将 UITextField 子类化为 MyTextField,并覆盖 UIResponderStandardEditActions.[=20= 中的 -(void)copy:-(void)paste: 方法]

在您实施这些方法时,您应该使用您的自定义 UIPasteboard 将文本复制到其中并从中粘贴,如本回答中所述,

How to do copy/paste function programmatically in iphone?

但不使用 [UIPasteboard generalPasteboard],而是使用 [UIPasteboard pasteboardWithName:@"myPasteboard" create:YES]

//pasteboard initialization
let pasteBoard = UIPasteboard.general

// copying the content of textview to pasteboard
if(pasteBoard.hasStrings){
   pasteBoard.string = textViewPB.text
}

//pasting the content of pasteboard to textview
if(pasteBoard.hasStrings){
   textViewPB.text = pasteBoard.string
}