1.本地生成打印源
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"打印" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightClick:)];
a.网页
self.currentURL = @"http://www.baidu.com"; _webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:_webView]; NSURLRequest *url = [NSURLRequest requestWithURL:[NSURL URLWithString:self.currentURL]]; [_webView loadRequest:url];
b.字符串
string = @"寒冷关键是列宁格勒市个技能熟练感觉深谷隆司理工科麻省理工模式"
2.调用
-(void)rightClick:(id)sender{ UIPrintInteractionController *printC = [UIPrintInteractionController sharedPrintController];//显示出打印的用户界面。 printC.delegate = self; UIPrintInfo *printInfo = [UIPrintInfo printInfo];//准备打印信息以预设值初始化的对象。 printInfo.outputType = UIPrintInfoOutputGeneral;//设置输出类型。 printC.showsPageRange = YES;//显示的页面范围 // 打印网页 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.currentURL]]];//网页 printC.printFormatter = [self.webView viewPrintFormatter];//布局打印视图绘制的内容。 /* // 打印文本 UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"发噶感受噶sgsgsgsgsgsgsgsgsgsghghtgdnsgbvcgt"]; textFormatter.startPage = 0; textFormatter.contentInsets = UIEdgeInsetsMake(200, 300, 0, 72.0); // 插入内容页的边缘 1 inch margins textFormatter.maximumContentWidth = 16 * 72.0;//最大范围的宽 printC.printFormatter = textFormatter; */ // 打印完成 void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if (!completed && error) { NSLog(@"可能无法完成,因为印刷错误: %@", error); } }; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:sender];//调用方法的时候,要注意参数的类型-下面presentFromBarButtonItem:的参数类型是 UIBarButtonItem..如果你是在系统的UIToolbar or UINavigationItem上放的一个打印button,就不需要转换了。 [printC presentFromBarButtonItem:item animated:YES completionHandler:completionHandler];//在ipad上弹出打印那个页面 //[printC presentFromRect:CGRectMake(500, 500, 100, 200) inView:self.webView animated:YES completionHandler:completionHandler];//第二种方法 } else { //iPhone [printC presentAnimated:YES completionHandler:completionHandler];//在iPhone上弹出打印那个页面 } }
参考 http://blog.sina.com.cn/s/blog_8d1bc23f0102v39m.html
时间: 2024-12-08 16:40:13