How do I use Airprint to print a text from a Uitextfield?

Asked

Viewed 55 times

1

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

1 answer

2


Check if the device can print

Just check out isPrintingAvailable in the 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 to Property printingItem for the item you are going to print, it may be of the classes NSURL, NSData, UIImage, or ALAsset

Using plain text you can simply do

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

Create a Completion Handler if you feel it is 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 printing 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 one 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

  • at the beginning, have a check if it is possible to print. In this case, what can make the printing impossible? That is, what can do the if ([UIPrintInteractionController isPrintingAvailable]) return NO?

  • 1

    TIP: Hardware test kit for Xcode: https://developer.apple.com/downloads/index.action?name=hardware%20io%20tools#

Browser other questions tagged

You are not signed in. Login or sign up in order to post.