我们看看打印出来的效果

打印小票数据处理,针对服务器返回的数据进行数据封装成model dataPrint是服务器返回的json数据

- (HLPrinter *)getPrinter{PrinterModel *printModel = [PrinterModel mj_objectWithKeyValues:dataPrint];HLPrinter *printer = [[HLPrinter alloc] init];[printer appendText:[NSString stringWithFormat:@"【%@】",printModel.orderTitle] alignment:HLTextAlignmentCenter];[printer appendText:@"- - 已支付 - -" alignment:HLTextAlignmentCenter];[printer appendSeperatorLine];[printer appendTitle:@"订单编号 :" value:printModel.orderNumber valueOffset:130];[printer appendTitle:@"下单时间 :" value:printModel.orderCreateTime valueOffset:130];[printer appendSeperatorLine];NSMutableString *customerName = [[NSMutableString alloc] initWithString:printModel.customerName];if(customerName.length >= 36){ // 客户名称[customerName substringToIndex:36];[customerName deleteCharactersInRange:NSMakeRange(36, customerName.length-36)];[customerName insertString:@"            " atIndex:18];}else if (customerName.length >= 18) {[customerName insertString:@"            " atIndex:18];}[printer appendTitle:@"客户名称 :" value:customerName valueOffset:130];[printer appendTitle:@"单据类型 :" value:printModel.orderType valueOffset:130];[printer appendTitle:@"销售人员 :" value:printModel.salesMan valueOffset:130];[printer appendTitle:@"销售部门 :" value:printModel.salesDepartment valueOffset:130];[printer appendTitle:@"发货仓库 :" value:printModel.warehouse valueOffset:130];[printer appendSeperatorLine];[printer appendFourLinesText:@"商品名称" middleText:@"数量" rightText:@"单位" moneyText:@"价格(元)" isTitle:YES];NSMutableArray *arrProduct = [self showProductData:printModel]; // 拼装商品信息for (ProductBeans *product in arrProduct) {[printer appendFourLinesText:product.productName middleText:product.productCount rightText:product.productUnit moneyText:product.productPrice  isTitle:NO];}[printer appendSeperatorLine];[printer appendSubtotalText:@"小计" centerText:printModel.productCount rightText:printModel.productTotalPrice isTitle:YES];[printer appendSeperatorLine];if (printModel.giftBeans.count > 0) { // 赠送商品大于0显示赠品[printer appendText:@"【赠送商品】" alignment:HLTextAlignmentCenter];[printer appendNewLine];[printer appendLeftText:@"商品名称" middleText:@"数量" rightText:@"单位" isTitle:YES];NSMutableArray *arrGift = [self showGiftData:printModel]; //拼装赠送商品信息for (GiftBeans *gift in arrGift) {[printer appendLeftText:gift.productName middleText:gift.productCount rightText:gift.productUnit isTitle:NO];}[printer appendSeperatorLine];[printer appendTitle:@"小计" value:printModel.giftCount valueOffset:500 - 62];[printer appendSeperatorLine];}CGFloat titleWidth = [printModel.totalPrice boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11 weight:UIFontWeightRegular]} context:nil].size.width;[printer appendTitle:@"实付金额 :" value:printModel.totalPrice valueOffset:520 - (titleWidth)];[printer appendNewLine];[printer appendNewLine];[printer appendNewLine];[printer appendNewLine];// 你也可以利用UIWebView加载HTML小票的方式,这样可以在远程修改小票的样式和布局。// 注意点:需要等UIWebView加载完成后,再截取UIWebView的屏幕快照,然后利用添加图片的方法,加进printer// 截取屏幕快照,可以用UIWebView+UIImage中的catogery方法 - (UIImage *)imageForWebViewreturn printer;
}

针对商品名称进行换行逻辑处理,把原数据进行组装

/// 商品名称
- (NSMutableArray *)showProductData:(PrinterModel *)printModel{NSMutableArray *mArray = [[NSMutableArray alloc]initWithCapacity:0];NSInteger  priceLength = 0;NSInteger  unitLength = 0;NSInteger  countLength = 0;for (int i=0; i<printModel.productBeans.count; i++) {ProductBeans *model = printModel.productBeans[i];if (model.productUnit.length>unitLength) {unitLength = model.productUnit.length;}if (model.productPrice.length>priceLength) {priceLength = model.productPrice.length;}if (model.productCount.length>countLength) {countLength = model.productCount.length;}NSString *name = model.productName;if (name.length >= 28) {for (int i=0; i<3; i++) { // 三行时ProductBeans *subModel = [[ProductBeans alloc]init];if (i == 0) {subModel.productName = [name substringWithRange:NSMakeRange(0, 14)];subModel.productCount = model.productCount;subModel.productUnit = model.productUnit;subModel.productPrice = model.productPrice;}else if (i == 1) {subModel.productName = [name substringWithRange:NSMakeRange(14, 14)];subModel.productCount = @"";subModel.productUnit = @"";subModel.productPrice = @"";}else {if (name.length > 42) {subModel.productName = [name substringWithRange:NSMakeRange(28, 14)];}else{subModel.productName = [name substringWithRange:NSMakeRange(28, name.length-28)];}subModel.productCount = @"";subModel.productUnit = @"";subModel.productPrice = @"";}[mArray addObject:subModel];}}else if (name.length >= 14) {for (int i=0; i<2; i++) { // 两行时ProductBeans *subModel = [[ProductBeans alloc]init];if (i == 0) {subModel.productName = [name substringWithRange:NSMakeRange(0, 14)];subModel.productCount = model.productCount;subModel.productUnit = model.productUnit;subModel.productPrice = model.productPrice;}else {if (name.length > 28) {subModel.productName = [name substringWithRange:NSMakeRange(14, 14)];}else{subModel.productName = [name substringWithRange:NSMakeRange(14, name.length-14)];}subModel.productCount = @"";subModel.productUnit = @"";subModel.productPrice = @"";}[mArray addObject:subModel];}}else {[mArray addObject:model];}}printModel.productBeans = mArray;for (int i=0; i<printModel.productBeans.count; i++) {ProductBeans *model = printModel.productBeans[i];NSMutableString *unit = [NSMutableString stringWithString:model.productUnit];if (unit.length < unitLength) {NSInteger length = unitLength - unit.length;for (int i=0; i<length; i++) {[unit insertString:@" " atIndex:0];}model.productUnit = unit;}NSMutableString *price = [NSMutableString stringWithString:model.productPrice];if (price.length < priceLength) {NSInteger length = priceLength - price.length;for (int i=0; i<length; i++) {[price insertString:@" " atIndex:0];}model.productPrice =  price;}NSMutableString *count = [NSMutableString stringWithString:model.productCount];if (count.length < countLength) {NSInteger length = countLength - count.length;for (int i=0; i<length; i++) {[count insertString:@" " atIndex:0];}model.productCount = count;}}return mArray;
}

针对赠送的商品进行数据组装处理

/// 赠送信息
- (NSMutableArray *)showGiftData:(PrinterModel *)printModel{NSMutableArray *arraygift = [[NSMutableArray alloc]initWithCapacity:0];NSInteger  priceLength = 0;NSInteger  unitLength = 0;NSInteger  countLength = 0;for (int i=0; i<printModel.giftBeans.count; i++) {GiftBeans *model = printModel.giftBeans[i];if (model.productUnit.length>unitLength) {unitLength = model.productUnit.length;}if (model.productPrice.length>priceLength) {priceLength = model.productPrice.length;}if (model.productCount.length>countLength) {countLength = model.productCount.length;}NSString *name = model.productName;if (name.length >= 44) {for (int i=0; i<3; i++) { // 三行时GiftBeans *subModel = [[GiftBeans alloc]init];if (i == 0) {subModel.productName = [name substringWithRange:NSMakeRange(0, 22)];subModel.productCount = model.productCount;subModel.productUnit = model.productUnit;subModel.productPrice = model.productPrice;}else if (i == 1) {subModel.productName = [name substringWithRange:NSMakeRange(22, 22)];subModel.productCount = @"";subModel.productUnit = @"";subModel.productPrice = @"";}else {subModel.productName = [name substringWithRange:NSMakeRange(44, name.length-44)];subModel.productCount = @"";subModel.productUnit = @"";subModel.productPrice = @"";}[arraygift addObject:subModel];}}else if (name.length >= 22) {for (int i=0; i<2; i++) { // 两行时GiftBeans *subModel = [[GiftBeans alloc]init];if (i == 0) {subModel.productName = [name substringWithRange:NSMakeRange(0, 22)];subModel.productCount = model.productCount;subModel.productUnit = model.productUnit;subModel.productPrice = model.productPrice;}else {subModel.productName = [name substringWithRange:NSMakeRange(22, name.length-22)];subModel.productCount = @"";subModel.productUnit = @"";subModel.productPrice = @"";}[arraygift addObject:subModel];}}else {[arraygift addObject:model];}}printModel.giftBeans = arraygift;for (int i=0; i<printModel.giftBeans.count; i++) {GiftBeans *model = printModel.giftBeans[i];NSMutableString *unit = [NSMutableString stringWithString:model.productUnit];if (unit.length < unitLength) {NSInteger length = unitLength - unit.length;for (int i=0; i<length; i++) {[unit insertString:@" " atIndex:0];}model.productUnit = unit;}NSMutableString *price = [NSMutableString stringWithString:model.productPrice];if (price.length < priceLength) {NSInteger length = priceLength - price.length;for (int i=0; i<length; i++) {[price insertString:@" " atIndex:0];}model.productPrice =  price;}NSMutableString *count = [NSMutableString stringWithString:model.productCount];if (count.length < countLength) {NSInteger length = countLength - count.length;for (int i=0; i<length; i++) {[count insertString:@" " atIndex:0];}model.productCount = count;}}return arraygift;
}

使用打印功能进行打印

- (void)blueButtonAction{NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey:@NO};//不弹窗(配置)self.manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
}#pragma mark 蓝牙状态Delegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {NSString *strMessage = @"";switch (central.state) {case CBManagerStatePoweredOn: {NSLog(@"蓝牙开启功能可用");[self showPrinterManager];return;}break;case CBManagerStatePoweredOff: {strMessage = @"手机蓝牙功能关闭,请前往设置打开蓝牙及控制中心打开蓝牙";}break;case CBManagerStateUnauthorized:{strMessage = @"访问蓝牙权限以用于设备数据读取和发送";}break;case CBManagerStateUnknown:break;case CBManagerStateResetting:break;case CBManagerStateUnsupported:break;}//通知没有打开蓝牙的自定义提示弹窗(弹窗代码自行实现)[self broadAlertMessage:strMessage];
}/// 设置弹框
/// @param strMessage 内容
- (void)broadAlertMessage:(NSString *)strMessage{UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:strMessage preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];UIAlertAction *queren = [UIAlertAction actionWithTitle:@"前往设置"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {[[UIApplication sharedApplication]openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{}completionHandler:^(BOOL  success) {}];}];[alert addAction:cancel];[alert addAction:queren];[self.navigationController presentViewController:alert animated:YES completion:nil];}///  蓝牙开启功能可用打印
- (void)showPrinterManager{SEPrinterManager *_manager = [SEPrinterManager sharedInstance];if (_manager.isConnected == NO) { // 去连接PrinterViewController *printer = [[PrinterViewController alloc] initWithNibName:@"PrinterViewController" bundle:nil];[self.navigationController pushViewController:printer animated:YES];}else{ // 去打印HLPrinter *printer = [self getPrinter];NSData* imageData = [printer getFinalData];// 对象转成字节流 发送给打印机kWeakify(self);if (imageData.length == 0) {[GHSProgressHUD showMessage:@"数据不能为空"];return;}[_manager sendPrintData:imageData completion:^(CBPeripheral *connectPerpheral, BOOL completion, NSString *error) {NSLog(@"写入结:%d---错误:%@",completion,error);if (completion == YES) {[weakSelf sendPrintInfo];}[GHSProgressHUD showSuccess:error];}];}}

我们样式和这个库要求不一样,我是基于这个库 进行打印,然后修改了内部格式和方法,我基于这个库修改了字体的行间距,计算宽度,2行3行4行不同样式时的处理

//行间距
Byte lineSpace[] = {0x1B,0x33,0x50};
/// 小计 三行
- (void)appendSubtotalText:(NSString *)left centerText:(NSString *)center rightText:(NSString *)right isTitle:(BOOL)isTitle
{[self setAlignment:HLTextAlignmentLeft];[self setFontSize:HLFontSizeTitleSmalle];NSInteger offset = 0;if (!isTitle) {offset = 10;}if (left) {[self setText:left];}CGFloat titleWidth = [right boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11 weight:UIFontWeightRegular]} context:nil].size.width;if (right) {[self setOffset:520 - (titleWidth)];[self setText:right];}if (center) {[self setOffset:500 - 235];[self setText:center];}[self appendNewLine];}
/// 三行 赠送商品
- (void)appendLeftText:(NSString *)left middleText:(NSString *)middle rightText:(NSString *)right isTitle:(BOOL)isTitle
{[self setAlignment:HLTextAlignmentLeft];[self setFontSize:HLFontSizeTitleSmalle];NSInteger offset = 0;if (!isTitle) {offset = 10;}if (left) {[self setText:left];}CGFloat titleWidth = [right boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11 weight:UIFontWeightRegular]} context:nil].size.width;if (right) {[self setOffset:520 - (titleWidth)];[self setText:right];}if (middle) {[self setOffset:520 - (titleWidth + 70)];[self setText:middle];}[self appendNewLine];}
/// 四行商品 名称 数量 单位 价格
- (void)appendFourLinesText:(NSString *)left middleText:(NSString *)middle rightText:(NSString *)right moneyText:(NSString *)money isTitle:(BOOL)isTitle{[self setAlignment:HLTextAlignmentLeft];[self setFontSize:HLFontSizeTitleSmalle];NSInteger offset = 0;if (!isTitle) {offset = 10;}if (left) {[self setText:left];}CGFloat rightWidth = [right boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 18) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:11 weight:UIFontWeightRegular]} context:nil].size.width;if (money) {[self setOffset:500 - (rightWidth + 10)];[self setText:money];}if (right) {[self setOffset:500 - (rightWidth + 10 + 80)];[self setText:right];}if (middle) {[self setOffset:500 - (rightWidth + 10 + 80 + 80)];[self setText:middle];}[self appendNewLine];}

对打印小票感兴趣的可以看看 蓝牙打印库https://github.com/Haley-Wong/HLBluetoothDemohttps://github.com/Haley-Wong/HLBluetoothDemo

iOS蓝牙连接打印机,打印小票相关推荐

  1. Android 蓝牙连接打印机打印网络图片

    实现蓝牙连接打印机打印网络图片 经过自己一下午加一个小时的时间整理出来,希望能帮助到各位码兄弟! 主要分为以下几步: 将网络图片URL转为bitmap :其中需要进行网络请求,不可在主线程中进行,需另 ...

  2. 蓝牙连接打印机打印资产标签.

    新出一个需求,打印资产标签,扫描标签中二维码的时候,根据二维码内容展示资源详细信息; 二维码打印就不说了,蓝牙连接便携打印机,然后根据打印机厂家的API,排版打印即可,打印机厂家是杭州舜普. 贴一下便 ...

  3. uniapp APP实现通过蓝牙连接打印机打印

    蓝牙连接德佟打印机打印 1.导入插件: 在插件市场中搜索LPAPI,进入之后,点击右侧的"购买for云打包",选择目标项目,按照提示操作即可: 2. 配置插件: 用HBuilder ...

  4. JAVA连接打印机打印小票、产品信息。标签打印机

    2018年写了一个标签打印机(TSC TX600),标签打印程序,实现功能是通过串口读出设备ID生成二维码和设备信息,用于esp8266 smart config配网.源码和资料:https://do ...

  5. 蓝牙连接打印机打印文字图片条形码二维码 用的是Gprinter打印机

    实现了蓝牙打印,正在进一步完善,欢迎留言交流 :) Github链接

  6. Android手机蓝牙连接热敏打印机 打印票据

    手机蓝牙连接热敏打印机 打印票据 话不多说上代码: 项目地址:可直接作为项目依赖 引用 allprojects {repositories {...maven { url 'https://jitpa ...

  7. 自己的电脑不能连接打印机打印怎么办

    近来,自己购买了一台小型的打印机,原本想着打印一些比较简单的东西,可是没有想到使用过程中困难多多,卡纸.打印字迹不清晰等问题一件件接着来,我开始怀疑难道我购买的打印机质量太差劲了吗? 相信不少人在打印 ...

  8. python连接打印机打印文档、图片、pdf文件等

    引言 python连接打印机进行打印,可能根据需求的不同,使用不同的函数模块. 如果你只是简单的想打印文档,比如office文档,你可以使用ShellExecute方法,对于微软office的文档.p ...

  9. NWJS(NodeJS)调用打印机 - 打印小票

    1 背景 架构设计:VueJS + Spring Cloud微服务架构 功能要求: 调用小票打印机打印小票,功能和超市收银结算功能相同 使用NWJS包装VueJS前端代码实现exe安装包和可执行文件 ...

  10. Secure CRT自动连接打印机打印乱码问题

    最近发现了一件怪事,发现自己的电脑会自动连接打印机打印文件. 关键是打印出来的都是乱码,一行行的电波文,看的脑阔疼. 刚开始还不知道是什么原有导致的. 后来一狠心把打印机给删除了. 第二天,过了一段时 ...

最新文章

  1. 如何用计算机打出love,游戏中名字的LOVE怎么用符号打出来?
  2. 计算机视觉:单阶段目标检测模型YOLO-V3
  3. linux下搭建ntp服务,Linux 下快速搭建ntp 时间同步服务器
  4. 转发2篇大学生写的博文---看了比较有感触
  5. 「权威发布」2019年大学生电子设计竞赛,仪器设备和主要元器件清单
  6. Not so Mobile(二叉树递归输入同时建树){天平}
  7. STM32之串口DMA例程
  8. Nginx使用HTTPS建立与上游服务器的网络通信
  9. bzoj1854: [Scoi2010]游戏 贪心
  10. 【Flink Forward Asia 2021】活动报告出炉,实时即未来!
  11. 20190910每日一句 你有勇气直面自己的恐惧吗?
  12. C/C++[codeup 1967]数组逆置
  13. Springboot的工作机制:2 @SpringBootApplication背后的秘密
  14. 软件工程领域2021年上半年的CCF-A和B类会议列表
  15. 软考中级软件设计师基础知识总结
  16. 【毕设】selenium 爬取知网作者信息
  17. 头歌 Java IO 答案 增加章节java学习-Java输入输出之字节缓冲IO流之复制文件
  18. 饿了么小程序容器首屏秒开优化实践
  19. Ansible详解(一)
  20. 联想微型计算机c325参数,联想一体机c325性能表现 联想一体机c325配置参数

热门文章

  1. 值得收藏!教你如何在火星直播中使用分享码
  2. Android 三类框架的理解以及MVVM框架的使用
  3. 数字信号处理 史林 课本答案---第三章
  4. CSS-div垂直居中方法总结
  5. 京东架构专家分享京东架构之路
  6. linux程序开发ide,LiteIDE 开发工具指南 (Go语言开发工具)
  7. 联想电脑如何进入BIOS的方法汇总
  8. 市场28款主流同步整流DCDC芯片横向测评预告
  9. DCDC基础(2)--BUCK芯片的各个引脚是什么意思?带你深入了解BUCK电源的稳压原理
  10. 最新消息,CDRX7冰点价再返现,你知道么?