Print Arrays Using printer in IOS

Source: Internet
Author: User

Problem description:


There are three nsmutable arrays in IOS. How can I use printer to print data similar to the following?

Example:


[Plain]
0.5 1 10
1 10 11
2 5 22
3 4 6

0.5 1 10
1 10 11
2 5 22
3 4 6
Solution:


Use UIPrintInteractionController to implement:

Code:

[Plain]
NSMutableString * stringToPrint = [[NSMutableString alloc] initWithString: @ ""];
For (int I = 0; I <[array count]; I ++)
{
[StringToPrint appendFormat: @ "% @", [array objectAtIndex: I];
If (I % 2 = 0)
{
[StringToPrint appendFormat: @ "\ n"];
}
Else
{
[StringToPrint appendFormat: @ "\ t"];
}
}
UIPrintInteractionController * pic = [UIPrintInteractionController sharedPrintController];
 
UIPrintInfo * printInfo = [UIPrintInfo printInfo];
PrintInfo. outputType = UIPrintInfoOutputGeneral;
PrintInfo. jobName = @ "Midhun ";
Pic. printInfo = printInfo;
 
UISimpleTextPrintFormatter * textFormatter = [UISimpleTextPrintFormatter alloc]
InitWithText: stringToPrint];
TextFormatter. startPage = 0;
TextFormatter. contentInsets = UIEdgeInsetsMake (72.0, 72.0, 72.0, 72.0); // 1 inch margins
TextFormatter. maximumContentWidth = 6*72.0;
Pic. printFormatter = textFormatter;
Pic. showsPageRange = YES;
 
Void (^ completionHandler) (UIPrintInteractionController *, BOOL, NSError *) =
^ (UIPrintInteractionController * printController, BOOL completed, NSError * error)
{
If (! Completed & error)
{
NSLog (@ "Printing cocould not complete because of error: % @", error );
}
};
If (UI_USER_INTERFACE_IDIOM () = UIUserInterfaceIdiomPad)
{
[Pic presentFromBarButtonItem: sender animated: YES completionHandler: completionHandler];
}
Else
{
[Pic presentAnimated: YES completionHandler: completionHandler];
}

NSMutableString * stringToPrint = [[NSMutableString alloc] initWithString: @ ""];
For (int I = 0; I <[array count]; I ++)
{
[StringToPrint appendFormat: @ "% @", [array objectAtIndex: I];
If (I % 2 = 0)
{
[StringToPrint appendFormat: @ "\ n"];
}
Else
{
[StringToPrint appendFormat: @ "\ t"];
}
}
UIPrintInteractionController * pic = [UIPrintInteractionController sharedPrintController];

UIPrintInfo * printInfo = [UIPrintInfo printInfo];
PrintInfo. outputType = UIPrintInfoOutputGeneral;
PrintInfo. jobName = @ "Midhun ";
Pic. printInfo = printInfo;

UISimpleTextPrintFormatter * textFormatter = [UISimpleTextPrintFormatter alloc]
InitWithText: stringToPrint];
TextFormatter. startPage = 0;
TextFormatter. contentInsets = UIEdgeInsetsMake (72.0, 72.0, 72.0, 72.0); // 1 inch margins
TextFormatter. maximumContentWidth = 6*72.0;
Pic. printFormatter = textFormatter;
Pic. showsPageRange = YES;

Void (^ completionHandler) (UIPrintInteractionController *, BOOL, NSError *) =
^ (UIPrintInteractionController * printController, BOOL completed, NSError * error)
{
If (! Completed & error)
{
NSLog (@ "Printing cocould not complete because of error: % @", error );
}
};
If (UI_USER_INTERFACE_IDIOM () = UIUserInterfaceIdiomPad)
{
[Pic presentFromBarButtonItem: sender animated: YES completionHandler: completionHandler];
}
Else
{
[Pic presentAnimated: YES completionHandler: completionHandler];
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.