ヒレガス本 21章 課題1

サンプルコードの雛形が欲しかったので、久しぶりにヒレがス本の課題の続きを行なった。

第21章でBigLetterViewにコピー&ペースト機能をつけた。

課題は、コピー時に、テキストだけでなくPDFもペーストボードに書き込めとのこと。

単純にwriteToPasteboardメソッドを書き換える。

- (void)writeToPasteboard:(NSPasteboard*)pb
{
    [pb clearContents];

    // pdfデータの準備
    NSData* thePDFData  = [self dataWithPDFInsideRect:self.bounds];

    NSPasteboardItem* thePDFItem = [[NSPasteboardItem alloc] init];
    [thePDFItem setData:thePDFData forType:NSPasteboardTypePDF];

    NSPasteboardItem* theTextItem = [[NSPasteboardItem alloc] init];
    [theTextItem setString:self.string forType:NSPasteboardTypeString];

    [pb writeObjects:@[thePDFItem, theTextItem]];
}

一種類のデータだけならば、NSPasteboardを使うだけで良いが、複数のデータを扱う場合は、NSPasteboardItemを使う必要がある。

プロジェクトファイルは、 TypingTutor_3.zip に置いておきます。