I am trying to add two NSAttributedStrings together. I want it to print out so that the title has a different font size and font than the body of the print. Here is the code I am using.
-(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!");
}];
}
When I print this the printer prints a message that looks like this and it just says all the font data.
This is a test title{
NSFont = <font data>
}
this is the body of the print
If any one could help that would be great!
This line:
doesn't really make much sense. Keep in mind that when you use
%@
in a string format, the value put there is from calling thedescription
method of the corresponding object. This is not the correct way to combine two attributed strings.So the first thing you need to fix is combining the two attributed strings. You want:
Now that you have the proper attributed string you wish to print, you need to change how you create the print formatter. Instead of:
you want: