簡體   English   中英

帶有NSAttributedString的AirPrint

[英]AirPrint with NSAttributedString

我正在嘗試將兩個NSAttributedStrings添加在一起。 我希望將其打印出來,以便標題具有與打印正文不同的字體大小和字體。 這是我正在使用的代碼。

-(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

如果有人可以幫助,那就太好了!

這行:

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

真的沒有多大意義。 請記住,當以字符串格式使用%@時,放置在其中的值來自調用相應對象的description方法。 這不是組合兩個屬性字符串的正確方法。

因此,您需要解決的第一件事是結合兩個屬性字符串。 你要:

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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM