首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AirPrint与NSAttributedString

AirPrint与NSAttributedString
EN

Stack Overflow用户
提问于 2016-05-13 16:43:30
回答 1查看 240关注 0票数 0

我正试图将两个NSAttributedStrings加在一起。我希望它打印出来,以便标题有一个不同的字体大小和字体与正文的打印。这是我正在使用的代码。

代码语言:javascript
复制
-(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!");
            }];

        }

当我打印这个时,打印机会打印一条看起来像这样的消息,它只显示所有的字体数据。

代码语言:javascript
复制
This is a test title{
NSFont = <font data>
}
this is the body of the print

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-13 17:58:04

这一行:

代码语言:javascript
复制
NSMutableString *printBody = [NSMutableString stringWithFormat:[NSString stringWithFormat:@"%@ \n%@ \n",title,body]];

没有多大意义。请记住,当您在字符串格式中使用%@时,其中的值来自于调用相应对象的description方法。这不是组合两个属性字符串的正确方法。

因此,您需要修复的第一件事是将两个属性化字符串组合起来。你想:

代码语言:javascript
复制
NSAttributedString *newline = [[NSAttributedString alloc] initWithString:@"\n"];
NSMutableAttributedString *printBody = [title mutableCopy];
[printBody appendAttributedString:newline];
[printBody appendAttributedString:body];
[printBody appendAttributedString:newline];

既然您有了要打印的适当属性化字符串,就需要更改创建打印格式化程序的方式。而不是:

代码语言:javascript
复制
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];

你想:

代码语言:javascript
复制
UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithAttributedText:printBody];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37215310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档