【问题标题】:AirPrint with NSAttributedString带有 NSAttributedString 的 AirPrint
【发布时间】:2016-05-13 16:43:30
【问题描述】:

我正在尝试将两个 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

如果有人能帮忙那就太好了!

【问题讨论】:

  • 您为什么不尝试以 HTML 格式打印出来?

标签: ios objective-c airprint uiprintformatter


【解决方案1】:

这一行:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多