這是我的粘貼文本的實驗。我正在使用一個按鈕以編程方式添加文本。
#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]);
}
我粘貼內容到一個文本字段,檢查,每一次,以確保以前的貼吧不只是重新使用改變了文本內容。
我可以問爲什麼你這樣做而不是使用 - [UIPasteboard setString:]方法?另外,你什麼意思是「什麼都沒有發生?」你期待什麼發生?你怎麼確定這個? – 2009-06-29 11:08:30