RasterizedReceiptPrinting
使用命令模式向收据打印机打印Unicode不可靠。迄今为止的最佳解决方案是将打印内容转换为UIImage,然后将整个图像发送到打印机。采用这种方式,开发者可以完全控制Unicode的打印工作,并便于管理布局。然而,必须使用单字体来使对齐更加精确。
使用方法
首先,我们必须创建一个NSAttributedString
。
-
实现Swift:
let printTemplate = NSMutableAttributedString(string: "")
然后,我们可以像这样为该模板添加新的属性字符串。
-
实现Swift:
printTemplate.append("Hello World!!!\n") printTemplate.appendCenter("This is a center body.\n") printTemplate.appendBold("End World!!!\n")
最后,创建具有自定义字符串和页面宽度设置的图像。
-
实现Swift:
let image = printTemplate.rasterize(width: 576)
恭喜!您已完成。
安装
RasterizedReceiptPrinting通过CocoaPods提供。要安装它,只需将以下行添加到Podfile中即可
pod 'RasterizedReceiptPrinting'
在模块使用之前
Swift
在您的 Swift 文件中添加以下行:
import RasterizedReceiptPrinting
示例
要运行示例项目,请先克隆仓库,然后从 Example 目录运行 pod install
。
要求
- Swift 4.0
- iOS 9.0 或更新版本
作者
- Chinh Nguyen,[email protected]
- Tien Pham,[email protected]
许可证
RasterizedReceiptPrinting 在 MIT 许可证下可用。更多信息请参阅 LICENSE 文件。