iOS UIPrintInteractionController打印
- (void)printData{ //为打印做准备,创建一个指向sharedPrintController的引用
UIPrintInteractionController *printer = [UIPrintInteractionController sharedPrintController];
printer.delegate = self; //配置打印信息
UIPrintInfo *Pinfo = [UIPrintInfo printInfo];
Pinfo.outputType = UIPrintInfoOutputGeneral;//可打印文本、图形、图像
Pinfo.jobName = @"Print for xiaodui";//可选属性,用于在打印中心中标识打印作业
Pinfo.duplex = UIPrintInfoDuplexLongEdge;//双面打印绕长边翻页,NONE为禁止双面
Pinfo.orientation = UIPrintInfoOrientationPortrait;//打印纵向还是横向 // Pinfo.printerID = @"";//指定默认打印机,也可以使用UIPrintInteractionControllerDelegate来知悉
printer.printInfo = Pinfo; //设置页面范围 打印文字
// UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"哈护手霜按时哈哈"]; // textFormatter.startPage = 0;//指定从哪一张开始打印0代表第一张 // textFormatter.contentInsets = UIEdgeInsetsMake(36, 36, 36, 36);//72相当于1英寸,这样设置上下左右的边距都为0.5英寸 // textFormatter.maximumContentWidth = 504;//(72x7.5)相当于打印宽度为7英寸 // printer.printFormatter = textFormatter;
/*
- AirPrint可以直接打印一些内容。 这些内容是 NSData, NSURL, UIImage, and ALAsset 类的实例, 但是这些实例的内容, 或者引用的类型(NSURL)必须是 image 或者pdf.
- 对于 image来说, NSData, NSURL, UIImage, and ALAsset 类型都可以的。 对于PDF, 只能使用 NSData, NSURL。 然后需要将这些数据实例直接赋值 给 UIPrintInteractionController实例的 printingItem 或者 printingItems 属性。
*/
// printer.printingItem = [UIImage imageNamed:@"LaunchImage"];
printer.printingItems = @[[UIImage imageNamed:@"welcome_page2"], [UIImage imageNamed:@"LaunchImage"], [UIImage imageNamed:@"welcome_page1"],]; printer.showsPageRange = NO; [printer presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) { if (!completed && error) {
NSLog(@"Error");
}
}]; }
打印webView视图
初始化webView
- (UIWebView *)printWebView
{
if (!_printWebView) { _printWebView = [[UIWebView alloc] initWithFrame:CGRectMake(, , Main_Screen_Width, Main_Screen_Height)]; _printWebView.backgroundColor = HexRGB(0xf9f9f9); _printWebView.delegate = self; _printWebView.scalesPageToFit = YES;
}
return _printWebView;
}
加载视图
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.printWebView loadRequest:request];
在加载完成中调用打印方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self printWebViewPage];
} - (void)printWebViewPage{ UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if(error){
NSLog(@"FAILED! due to error in domain %@ with error code %ld-- %@", error.domain, (long)error.code, completionHandler);
}
}; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.jobName = @"Print for xiaodui";; printInfo.duplex = UIPrintInfoDuplexLongEdge; controller.printInfo = printInfo; controller.showsPageRange = YES; UIViewPrintFormatter *viewFormatter = [self.printWebView viewPrintFormatter]; viewFormatter.startPage = ; controller.printFormatter = viewFormatter; [controller presentAnimated:YES completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {
if (!completed && error) {
NSLog(@"Error");
}
}];
}
如图:
iOS UIPrintInteractionController打印的更多相关文章
- iOS——关于打印控件
20.UIPrintFormatterUIPrintFormatter时打印格式化的抽象基类:展示了传统的可打印的内容对象可以跨页边界.由于打印格式化,打印系统,可以自动打印与打印格式化的内容相关联的 ...
- iOS UIPrintInteractionController在iPad的 iOS10 和 11上的奇怪bug
今天在弹出UIPrintInteractionController的时候,在ios10 和11的ipad 上测试,发现一直是protrait 方向弹出,结果就出现如下图的bug: 研究了好长时间,发现 ...
- iOS AFNetworking 打印从服务器返回的错误提示信息
每次做项目的时候都会在网络请求时候测试接口的时候会出现一些不同的错误,而控制台打印的错误提示信息都是data类型,看不出提示的错误的信息是什么.后面经过一些查阅发现其实是可以把这个转变为string的 ...
- iOS中打印系统详细日志
Q:如何打印当前的函数和行号? A:我们可以在打印时使用一些预编译宏作为打印参数,来打印当前的函数和行号.如: 1 NSLog(@"%s:%d obj=%@", __func__, ...
- ios runtime 打印内 内部调用的属性
unsigned int count = 0; // 拷贝出所有的成员变量列表 Ivar *ivars = class_copyIvarList([UITextField class], &c ...
- iOS - OC - 打印信息 - xcode 中文打印
#import <Foundation/Foundation.h> @implementation NSDictionary (Log) //重写系统的方法控制输出 -(NSString ...
- iOS字体打印
//打印所以字体 NSArray *familyNames = [UIFont familyNames]; for(NSString *familyName in familyNames) ...
- iOS 控制台打印unicode 转中文汉字 UTF8String
今天查看代码数据结构,就在控台直接打印了,soGa,我看到了什么!!!! 于是乎想到了不对劲,不对呀,之前打印都是 UTF8String的呀,怎么会这样,百思不得其姐,看了一下封装的网络类,SoGa, ...
- ios nslog 打印字典为中文
#import <Foundation/Foundation.h> @implementation NSDictionary (Log) - (NSString *)description ...
随机推荐
- MFC不同工程(解决方案)之间对话框资源的复制与重用方法(转)
原文转自 https://blog.csdn.net/lihui126/article/details/45556687
- 利用itext将html转为pdf
亲测代码没有问题,需要注意细节已经标注:需要jar包:iText-2.0.8.jar:core-renderer-R8.jar: core-renderer-R8.jar下载地址:http://cen ...
- 工具安装===Sublime Text-安装
Sublime Text 是一款通用型轻量级编辑器,支持多种编程语言.有许多功能强大的快捷键(如 Ctrl+d),支持丰富的插件扩展.如果平时需要在不同编程语言间切换,那么它将会是一个,不错的选择. ...
- python内建方法
abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce com ...
- openstack前期准备
. 两台虚拟机,安装Centos7系统 两个网卡 -- 一个NAT模式,一个仅主机模式 两个硬盘 -- 一个20GB,一个50GB 内存 -- 主 .6GB(根据自己的配置,大于2G即可) 从 1.6 ...
- redis的安装和php的redis扩展
一.redis的安装和配置 1.官方现在源码 https://redis.io/download 2.解压源码 tar zxvf redis-3.2.11.tar.gz 3.编译 make 编译 ...
- ltsdangerous加密解密
前言 在做QQ第三方登录时,用户跳转到QQ登录界面登录成功后,会在URL返回一个code参数.前端把code发送给后端.后端收到后,会查询出openid.然后判断openid是否存在,如果存在就可以绑 ...
- linux命令(18):chmod命令
1. 命令格式: chmod [-cfvR] [--help] [--version] mode file 2. 命令功能: 用于改变文件或目录的访问权限,用它控制文件或目录的访问权限. 3. 命令参 ...
- Java读写锁(ReentrantReadWriteLock)学习
什么是读写锁 平时,我们常见的synchronized和Reentrantlock基本上都是排他锁,这些锁在同一时刻只允许一个线程进行访问,哪怕是读操作.而读写锁是维护了一对锁(一个读锁和一个写锁), ...
- git clone命令
从远程clone一个仓库 ...知识浅薄 git clone都发现貌似用的不顺 因为我有几个git账号 但是我也不知道就是git账号是怎么保存在终端上的 所以当我需要用一个新的github账号来clo ...