这是我将文本粘贴到粘贴板上的实验。我正在使用一个按钮以编程方式添加文本。
#import <MobileCoreServices/MobileCoreServices.h>
- (IBAction)setPasteboardText:(id)sender
{
UIPasteboard *pb = [UIPasteboard generalPasteboard];
NSString *text = @"東京京都大阪";
// Works, but generates an incompatible pointer warning
[pb setValue:text forPasteboardType:kUTTypeText];
// Puts generic item (not text type), can't be pasted into a text field
[pb setValue:text forPasteboardType:(NSString *)kUTTypeItem];
// Works, even with non-ASCII text
// I would say this is the best way to do it with unknown text
[pb setValue:text forPasteboardType:(NSString *)kUTTypeText];
// Works without warning
// This would be my preferred method with UTF-8 text
[pb setValue:text forPasteboardType:(NSString *)kUTTypeUTF8PlainText];
// Works without warning, even with Japanese characters
[pb setValue:text forPasteboardType:@"public.plain-text"];
// Works without warning, even with Japanese characters
[pb setValue:text forPasteboardType:@"public.text"];
// Check contents and content type of pasteboard
NSLog(@"%@", [pb items]);
}
我将内容粘贴到文本字段中进行检查,并每次更改文本内容以确保它不只是重复使用以前的粘贴。