パスワードを忘れた? アカウント作成
30234 journal

hylomの日記: WebViewで印刷時にheader/footerを付ける・完結編

日記 by hylom

さんざん悩んだ、WebViewでの印刷時にheader/footerを付ける方法、適切と思われる解決法が分かった。

まず、WebUIDelegeteの「webView:drawHeaderInRect:」や「webViewFooterHeight:」を適当なクラスに実装する(例:下記のソースコード1)。厳密にMVCに沿うならコントローラに実装するのがよいのかな?

webViewHeaderHeight:はヘッダの高さを計算してreturnするメソッド。このメソッドの戻り値がヘッダの高さになる。webView:drawHeaderInRect:は描画ルーチン。引数のNSRectが描画エリア。

続いて、実装したクラスのインスタンスを生成してWebViewのsetUIDelegate:メソッドを使ってDelegateとして登録する。

以上。下記のソースコード1はヘッダにWebViewで開いているページのタイトル、フッタにページ番号を入れる例。これだけだとヘッダ/フッタとHTML本文の間隔がないので適当にヘッダ/フッタサイズを調整しよう。

(ソースコード1)

- (void)webView:(WebView *)sender drawHeaderInRect:(NSRect)rect {
    NSString* str = [sender mainFrameTitle];
 
    NSMutableAttributedString* aStr = [[[NSMutableAttributedString alloc] init] autorelease];
    NSAttributedString *tmpAttrStr;
    NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
 
    [attributes setObject:[NSFont fontWithName:@"HiraKakuPro-W3" size:7.0f] forKey:NSFontAttributeName];
    tmpAttrStr = [[[NSAttributedString alloc] initWithString:str attributes: attributes] autorelease];
    [aStr appendAttributedString:tmpAttrStr];
 
    [self lockFocus];
    [aStr drawInRect: rect];
    [self unlockFocus];
 
}
 
- (void)webView:(WebView *)sender drawFooterInRect:(NSRect)rect {
    int pageNum;
    pageNum = [[NSPrintOperation currentOperation] currentPage];
    NSString* strPage = [[[NSString alloc] initWithFormat:@"-%d-", pageNum] autorelease];
 
    NSMutableAttributedString* aStr = [[[NSMutableAttributedString alloc] init] autorelease];
    NSAttributedString *tmpAttrStr;
    NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
 
    NSMutableParagraphStyle* style = [[[NSMutableParagraphStyle alloc] init] autorelease];
    [style setAlignment:NSCenterTextAlignment];
 
    [attributes setObject:[NSFont fontWithName:@"HiraKakuPro-W3" size:7.0f] forKey:NSFontAttributeName];
    [attributes setObject:style forKey:NSParagraphStyleAttributeName];
 
    tmpAttrStr = [[[NSAttributedString alloc] initWithString:strPage attributes: attributes] autorelease];
    [aStr appendAttributedString:tmpAttrStr];
 
    [self lockFocus];
    [aStr drawInRect: rect];
    [self unlockFocus];
}
 
- (float)webViewHeaderHeight:(WebView *)sender {
    NSString* str = [sender mainFrameTitle];
 
    NSMutableAttributedString* aStr = [[[NSMutableAttributedString alloc] init] autorelease];
    NSAttributedString *tmpAttrStr;
    NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
 
    [attributes setObject:[NSFont fontWithName:@"HiraKakuPro-W3" size:7.0f] forKey:NSFontAttributeName];
    tmpAttrStr = [[[NSAttributedString alloc] initWithString:str attributes: attributes] autorelease];
    [aStr appendAttributedString:tmpAttrStr];
 
    return [aStr size].height;
}
 
- (float)webViewFooterHeight:(WebView *)sender {
    int pageNum;
    pageNum = [[NSPrintOperation currentOperation] currentPage];
    NSString* strPage = [[[NSString alloc] initWithFormat:@"-%d-", pageNum] autorelease];
 
    NSMutableAttributedString* aStr = [[[NSMutableAttributedString alloc] init] autorelease];
    NSAttributedString *tmpAttrStr;
    NSMutableDictionary* attributes = [NSMutableDictionary dictionary];
 
    NSMutableParagraphStyle* style = [[[NSMutableParagraphStyle alloc] init] autorelease];
    [style setAlignment:NSCenterTextAlignment];
 
    [attributes setObject:[NSFont fontWithName:@"HiraKakuPro-W3" size:7.0f] forKey:NSFontAttributeName];
    [attributes setObject:style forKey:NSParagraphStyleAttributeName];
 
    tmpAttrStr = [[[NSAttributedString alloc] initWithString:strPage attributes: attributes] autorelease];
    [aStr appendAttributedString:tmpAttrStr];
 
    return [aStr size].height;
}

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

一つのことを行い、またそれをうまくやるプログラムを書け -- Malcolm Douglas McIlroy

読み込み中...