前言
-
从CSDN资源下载demo 源码:https://download.csdn.net/download/u011018979/14920529
1、应用场景:打印商品价格标签、打印交易小票 2、特色功能:实现自动连接最近使用的打印机、统一处理蓝牙状态 3、原理文章:https://kunnan.blog.csdn.net/article/details/85684014 4、解决的问题:人民币¥符号乱码的问题 private
-
效果图
价格标签打印(品名支持多行显示)
![]()
在这里插入图片描述
-
票据打印(自动实现%ns 自动补齐空格的功能)
![]()
在这里插入图片描述 ![]()
在这里插入图片描述
-
切换佳博打印机的打印模式方法
按住FEED键 和Pause键,再打开开机键开机,
I 、获取标签打印命令
-(NSData *)price_tscCommand{
TscCommand *command = [[TscCommand alloc]init];
[command addSize:40 :60];
[command addGapWithM:2 withN:0];
[command addReference:0 :0];
[command addTear:@"ON"];
[command addQueryPrinterStatus:ON];
[command addCls];
CGFloat marg = 10;
CGFloat topY = 5;
[command addTextwithX:200 withY:0+topY withFont:@"TSS24.BF2" withRotation:0 withXscal:1 withYscal:1 withText:@"商品名称"];// 字的高度30
// 间距10
//0、Barcode
[command addTextwithX:30 withY:30+marg+topY withFont:@"TSS24.BF2" withRotation:0 withXscal:1 withYscal:1 withText:@"2017.01.31"];
[command add1DBarcode:30 :70 :@"CODE128" :100 :1 :0 :2 :2 :@"1kn23456984"];
[command addTextwithX:47 withY:200+marg+topY withFont:@"TSS24.BF2"
withRotation:0 withXscal:1 withYscal:1 withText:@"6666.81/件"];// ¥
//1、¥
UIImage *image = [CtrlViewController imageFromText:@"¥" withFont:[UIFont systemFontOfSize:15] withColor:[UIColor blackColor]];
[command addBitmapwithX:30 withY:200+marg withMode:0 withWidth:15 withImage:image];
[command addPrint:1 :1];// M 行 n :列 打印两份
return [command getCommand];
}
1.1 解决人民币¥符号乱码的问题
/**
将文字转为UIImage,用于打印¥,来避免乱码
*/
+(UIImage *)imageFromText:(NSString*)text withFont: (UIFont *)font withColor:(UIColor *)color
{
CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(size,YES,0.0);
NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary*attribute = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:color
};
//1、针对打印机的特性:字体为黑色,因此设置背景颜色为b白色
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, 200, 200));
//2、将文字绘画进上下文
[text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil];
//3、从上下文获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
II、获取票据打印命令
2.1 门店票据:使用字符串格式化进行排版
完整代码请查看原文
2.2 避免乱码问题,推荐使用%n@ 进行格式化,而非%ns
NSString *text = [NSString stringWithFormat:@"%8@\t%8@\n",column1str,column2str];
-
不推荐的格式化方式
NSString *text = [NSString stringWithFormat:@"%8s\t%8s\n",column1str.UTF8String,column2str.UTF8String];
2.3 自动实现%ns 自动补齐空格的功能
完整代码请查看原文
III、 实现自动连接最近使用的打印机
- (void)connectLastPrint{
__weak __typeof__(self) weakSelf = self;
[KNConnecterManagerTool listenBluetoothpoweredState:^(CBPeripheral * _Nullable peripheral) {
return [weakSelf setupConnectPeripheral:peripheral];
}];
}
-(void) setupConnectPeripheral:(CBPeripheral *_Nullable)peripheral{
if ([peripheral.identifier.UUIDString isEqualToString:KBLUETOOTH]) {
NSLog(@"相同则开始连接此蓝牙");
[Manager connectPeripheral:peripheral options:nil timeout:50 connectBlack:^(ConnectState state) {
if (state == CONNECT_STATE_CONNECTED) {
[Manager stopScan];
// 修改状态
[self setupCONNECT_STATE_CONNECTED:peripheral];
}else if (state == CONNECT_STATE_TIMEOUT || state == CONNECT_STATE_FAILT || state == CONNECT_STATE_DISCONNECT){
if (state == CONNECT_STATE_DISCONNECT) {
[self setupCONNECT_STATE_DISCONNECT];
return ;
}
[SVProgressHUD showInfoWithStatus:@"请尝试重启蓝牙printer"];
[self connectLastPrint];
}
}
];
}
else{
NSLog(@"未找到该设备");
}
}
/**
* 断开连接
*/
- (IBAction)disconnectAction:(id)sender {
[Manager close];
}
#pragma mark - ******** 连接成功之后的处理,以便connectLastPrint
完整代码请查看原文
宏
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define KBLUETOOTH [kUserDefaults valueForKey:@"blueTooth"]//商户连接过的打印
IV、常见问题
4.1 iOS蓝牙状态的处理【蓝牙关闭及未授权的处理】
-
iOS蓝牙状态的处理【蓝牙关闭及未授权的处理】 https://blog.csdn.net/z929118967/article/details/107671823
4.2 CBCentralManagerStateUnsupported
如果使用timeout 时间太长容易导致这个问题,修改为时间小些就可以,例如2。
[Manager connectPeripheral:peripheral options:nil timeout:50 connectBlack:^(ConnectState state) {
V、 see also
更多内容请关注公众号:iOS逆向
本文分享自微信公众号 - iOS逆向(code4ios)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。