AirPrint 的 Objective-C 代码 [英] Objective-C code for AirPrint

查看:32
本文介绍了AirPrint 的 Objective-C 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在objective-c中如何制作一个IBAction方法来打印带有AirPrint的UITextView?

How can make a IBAction method for printing a UITextView with AirPrint in objective-c?

推荐答案

检查是否可以打印:

if ([UIPrintInteractionController isPrintingAvailable])
{
    // Available
} else {
    // Not Available
}

点击按钮后打印:

-(IBAction) buttonClicked: (id) sender;
{
    NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@, %@",self.encoded.text, self.decoded.text];
    [printBody appendFormat:@"



Printed From *myapp*"];

     UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
     pic.delegate = self;

     UIPrintInfo *printInfo = [UIPrintInfo printInfo];
     printInfo.outputType = UIPrintInfoOutputGeneral;
     printInfo.jobName = self.titleLabel.text;
     pic.printInfo = printInfo;

     UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
     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;
     [textFormatter release];
     pic.showsPageRange = YES;

     void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
     ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
     if (!completed && error) {
     NSLog(@"Printing could not complete because of error: %@", error);
     }
     };

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

}

最初由 '87vert' 在 iPhone Dev SDK 发布:Airprint 教程 - 简单打印文件

Originally posted by '87vert' at iPhone Dev SDK: Airprint Tutorial - Simple Print File

这篇关于AirPrint 的 Objective-C 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆