How to use AirPrint to print text from a UITextField?

1

Someone could indicate a tutorial, or sample code explaining how to print a text from a UITextField.

    
asked by anonymous 27.01.2015 / 16:59

1 answer

2

Check if device can print

Just check isPrintingAvailable on UIPrintInteractionController :

if ([UIPrintInteractionController isPrintingAvailable]){
    // Chamar impressão
} else {
    // Fazer algo que você julgar interessante
}

Using the UIPrintInteractionController

Access the singleton using [UIPrintInteractionController sharedPrintController]

sharedPrintController = [UIPrintInteractionController sharedPrintController];

Seven printingItem for the item to be printed, can be of classes NSURL , NSData , UIImage , or ALAsset

Using simple text you can simply do

sharedPrintController.printingItem = [self.myUITextField.text dataUsingEncoding:NSUTF8StringEncoding];

Create a completion handler if you deem it necessary:

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
     ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
     if (!completed && error) {
         NSLog(@"Erro: %@", error);
     }
}

Show:

[sharedPrintController presentFromBarButtonItem:self.rightButton animated:YES completionHandler:completionHandler];

Optional

Using UIPrintInfo

You can set information about the print used to property printInfo of your controller

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
 printInfo.outputType = UIPrintInfoOutputGeneral;
 printInfo.jobName = "Nome do Job";
 sharedPrintController.printInfo = printInfo;

If you prefer you can use a UISimpleTextPrintFormatter

 UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithAttributedText:self.myUITextField.attributedText];
 textFormatter.startPage = 0;
 textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // margens de uma polegada
 textFormatter.maximumContentWidth = 6 * 72.0;
 sharedPrintController.printFormatter = textFormatter;
 sharedPrintController.showsPageRange = YES;

Inspired by: iPhone SDK - simple print file

    
27.01.2015 / 21:31