热门搜索 :
考研考公
您的当前位置:首页正文

iOS蓝牙打印小票

来源:东饰资讯网

最近搞了个蓝牙打印小票的小东西,先上效果


IMG_0132(20171221-162648).jpg

数据格式 json:

{
    "ShopName":"XX宜山路店",
    "ShopPhone":"电话:021-64701557",
    "ShopInfos":[
                 "公司名称:XX光学科技(上海)有限公司 地址:徐汇区XX路700号",
                 
                 "订单编号:KALJLAJGLAIJGLAKGJJLAGJ",
                 "销售时间:2016-04-27 10:01:50",
                 "配镜顾问:吴志鹏",
                 "顾客姓名:王小姐",
                 "顾客电话:12345678909",
                 "备注:KALJLAJGLAIJGLAKGJJLAGJ"
                ],
    "GoodsAttributes":[
                       "名称",
                       "规格",
                       "批号",
                       "有效期",
                       "单价",
                       "数量",
                       "小计"
                       ],
    "GoodsProducts":[
                     "XX时尚光学眼镜Ke1820-F01 亮深灰  \n              490.00 1 ¥490.00",
                     "XX镜布灰色  \n              490.00 1 ¥490.00",
                     "XX非球面树脂镜片(薄) 左眼 光度:-1.50 散光:无 轴位:无 瞳距:60  \n              490.00 1 ¥490.00",
                     "XX非球面树脂镜片(薄) 右眼 光度:-1.50 散光:无 轴位:无 瞳距:60, \n              490.00 1 ¥490.00"
    ],
    "PayInfos":[
                "支付宝支付:    ¥490.00",
                "  金额合计:    ¥490.00",
                "  促销抵扣:    ¥0.00",
                "  实收合计:    ¥490.00",
                "  欠款金额:    ¥0.00"
    ],
    "ServesInfo":"XX、XX品牌镜架2年质保 1.限非人为原因导致的镜架质量问题 2.按【XX装配标准 GB13511.1-2011】执行",
    
    "FootTitle":"扫码获取发票,欢迎再次惠顾!"
}

使用:

- (HLPrinter *)getPrinterWith:(id)Object
{
    HLPrinter *printer = [[HLPrinter alloc] init];
    //读取本地的json数据
    NSData *JSONData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app" ofType:@"json"]];
    NSDictionary *datadic = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];

    //店铺信息
    NSArray * ShopInfos = datadic[@"ShopInfos"];
    [printer appendImage:[UIImage imageNamed:@"ico180"] alignment:HLTextAlignmentCenter maxWidth:300];//图片大小
    NSString *title = datadic[@"ShopName"];
    NSString *str1 = datadic[@"ShopPhone"];
    [printer appendText:title alignment:HLTextAlignmentCenter fontSize:HLFontSizeTitleMiddle];
    [printer appendText:str1 alignment:HLTextAlignmentCenter];

    for (int i= 0; i<ShopInfos.count; i++) {
        [printer appendText:ShopInfos[i] alignment:HLTextAlignmentLeft];
    }
    [printer appendNewLine];//换行,打印空白行

    //商品规格信息
    NSArray * GoodsAttributes = datadic[@"GoodsAttributes"];
    [printer appendLeftTextArray:GoodsAttributes];
    //商品信息
    NSArray * GoodsProducts = datadic[@"GoodsProducts"];

    for (NSString *goodsInfo in GoodsProducts) {
        [printer appendText:goodsInfo alignment:HLTextAlignmentLeft];
    }
    [printer appendNewLine];

    //支付信息
    NSArray * PayInfos = datadic[@"PayInfos"];

    for (int i=0; i< PayInfos.count; i++){
        [printer appendText:PayInfos[i] alignment:HLTextAlignmentLeft offSet:200];//偏移量
    }
    [printer appendSeperatorLine];

    [printer appendText:datadic[@"ServesInfo"] alignment:HLTextAlignmentLeft];
    [printer appendSeperatorLine];

//    [printer appendQRCodeWithInfo:@""]// 不推荐,文本太长的话,二维码底部会显示不全
    [printer appendQRCodeWithInfo:datadic[@"QRCodeURL"] size:6 alignment:HLTextAlignmentCenter]; //size 调整二维码大小

    [printer appendNewLine];
    [printer appendText:datadic[@"FootTitle"] alignment:HLTextAlignmentCenter];
    [printer appendNewLine];

    return printer;
}

- (void)printDataText:(id)obj
{
    HLPrinter *printer = [self getPrinterWith:obj];

    NSData *mainData = [printer getFinalData];
    [[SEPrinterManager sharedInstance] sendPrintData:mainData completion:^(CBPeripheral *connectPerpheral, BOOL completion, NSString *error) {
        NSLog(@"写入结:%d---错误:%@",completion,error);
    }];

}
Top