2016-05-13 10 views
0

2つのNSAttributedStringを一緒に追加しようとしています。私はタイトルが印刷物の本体とは異なるフォントサイズとフォントを持つように印刷したい。ここに私が使用しているコードがあります。NSAttributedStringを使用したAirPrint

-(IBAction)print:(id)sender{ 
    UIFont *font = [UIFont fontWithName:@"Avenir Next Condensed" size:16.0]; 
     NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font 
                    forKey:NSFontAttributeName]; 
     NSAttributedString *title = [[NSAttributedString alloc] initWithString:@"this is the title" attributes:attrsDictionary]; 


UIFont *font2 = [UIFont systemFontOfSize:14.0]; 

NSDictionary *attrsDictionary2 = [NSDictionary dictionaryWithObject:font2 forKey:NSFontAttributeName]; 

     NSAttributedString *body = [[NSAttributedString alloc] initWithString:@"this is the body of the print" attributes:attrsDictionary2]; 

      NSMutableString *printBody = [NSMutableString stringWithFormat:[NSString stringWithFormat:@"%@ \n" 
@"%@ \n",title,body]]; 

      UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; 
      pic.delegate = self; 


      UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
      printInfo.outputType = UIPrintInfoOutputGeneral; 
      printInfo.jobName = @"PrintJob"; 
      pic.printInfo = printInfo; 


      UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody]; 
      textFormatter.startPage = 0; 
      textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); 
      textFormatter.maximumContentWidth = 6 * 72.0; 
      pic.printFormatter = textFormatter; 
      pic.showsPageRange = YES; 

      UIPrinter *SavedPrinterUserDefaults = [[NSUserDefaults standardUserDefaults] 
               objectForKey:@"SavedPrinter"]; 
      [pic printToPrinter:SavedPrinterUserDefaults 
      completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { 
       if (completed && !error) 
       NSLog(@"it printed!"); 
      }]; 

     } 

これを印刷すると、プリンタは次のようなメッセージを表示し、すべてのフォントデータを表示します。

This is a test title{ 
NSFont = <font data> 
} 
this is the body of the print 

いずれかが役に立つと助けてくれれば助かります。

+0

なぜあなたは試してみて、HTML形式でそれをプリントアウトしませんか? –

答えて

1

このライン:

NSMutableString *printBody = [NSMutableString stringWithFormat:[NSString stringWithFormat:@"%@ \n%@ \n",title,body]]; 

が本当にあまり意味がありません。 %@を文字列形式で使用する場合は、対応するオブジェクトのメソッドを呼び出すことができます。これは、属性付きの2つの文字列を組み合わせる正しい方法ではありません。

最初に修正する必要があるのは、2つの属性付き文字列を結合することです。あなたが欲しい:

NSAttributedString *newline = [[NSAttributedString alloc] initWithString:@"\n"]; 
NSMutableAttributedString *printBody = [title mutableCopy]; 
[printBody appendAttributedString:newline]; 
[printBody appendAttributedString:body]; 
[printBody appendAttributedString:newline]; 

は今、あなたはあなたが印刷したい適切な属性付きの文字列を持っていることを、あなたは、印刷フォーマッタを作成する方法を変更する必要があります。代わりに:

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody]; 

あなたが欲しい:

UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithAttributedText:printBody]; 
関連する問題