我有一个UITextView,我需要使用Swift AirPrint内容。我试图尽可能地调整Object-C代码,但我不知道Object-C(或者如何将它转换为Swift),所以它一直是一个很大的挑战。以下是我到目前为止的情况:
var pic:UIPrintInteractionController = .sharedPrintController()
var viewpf:UIViewPrintFormatter = myTextView.viewPrintFormatter()
var myData:NSData = (myTextView.text as NSString).dataUsingEncoding(NSUTF8StringEncoding)
pic.delegate = self
pic.showsPageRange = true
pic.printFormatter = viewpf
pic.printingItem = myData;
//if (UIPrintInteractionController.canPrintData(myData)) {
pic.presentAnimated(true, completionHandler: nil)
//}
如果我激活“if”语句当然会失败并且不会尝试打印。但是,如果我现在对其进行评论并强制进行打印尝试,则会打开打印机选择对话框,我选择启用AirPrint的打印机。我点击打印并与打印机通信,将其发送给打印机并退出...但没有任何打印。
如果它有帮助,这里是打印机模拟器的结果:
[25 / Jul / 2014:16:16:51 -0400] [客户端1]加密连接。 [25 / Jul / 2014:16:16:51 -0400] [客户1]来自的连接 [v1.fe80 :: 6676:baff:feb2:5e42 + en0]现已加密。
当我选择打印机时会发生输出,但当我选择打印时,没有其他内容出现。
以下是AppCode向我展示的一些额外信息,如果这有任何帮助:
2014-07-25 16:16:50.823 LotteryOddsBoost5 [2255:23613] - [PKPaperList matchedPaper:preferBorderless:withDuplexMode:didMatch:] paperToMatch = result = matchType = 0
2014-07-25 16:16:51.740 LotteryOddsBoost5 [2255:23613] - [PKPaperList matchedPaper:preferBorderless:withDuplexMode:didMatch:] paperToMatch = result = matchType = 0
2014-07-25 16:16:57.249 LotteryOddsBoost5 [2255:23613] - [PKPaperList matchedPaper:preferBorderless:withDuplexMode:didMatch:] paperToMatch = result = matchType = 0
任何帮助都会非常感激!我已经解决了很多创建这个程序的问题,但缺少Swift特定的文档和示例代码只是踢我的屁股!
非常感谢!
答案 0 :(得分:1)
我认为您通过提供视图格式化程序和要打印的数据来混淆它。尝试:
var pic:UIPrintInteractionController = .sharedPrintController()
var viewpf:UIViewPrintFormatter = myTextView.viewPrintFormatter()
pic.delegate = self
pic.showsPageRange = true
pic.printFormatter = viewpf
pic.presentAnimated(true, completionHandler: nil)
答案 1 :(得分:0)
以下是用于打印文本的Swift 4代码:
func print(text: String) {
let printController = UIPrintInteractionController.shared
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.outputType = UIPrintInfoOutputType.general
printController.printInfo = printInfo
let format = UIMarkupTextPrintFormatter(markupText: textWithNewCarriageReturns)
format.perPageContentInsets.top = 72
format.perPageContentInsets.bottom = 72
format.perPageContentInsets.left = 72
format.perPageContentInsets.right = 72
printController.printFormatter = format
printController.present(animated: true, completionHandler: nil)
}
或者,如果您有一个包含多个\ n回车符的长字符串,则需要先将所有\ n次出现用br /替换为UIMarkupTextPrintFormatter。这是Swift 4的一个例子:
func print(text: String) {
let textWithNewCarriageReturns = text.replacingOccurrences(of: "\n", with: "<br />")
let printController = UIPrintInteractionController.shared
let printInfo = UIPrintInfo(dictionary: nil)
printInfo.outputType = UIPrintInfoOutputType.general
printController.printInfo = printInfo
let format = UIMarkupTextPrintFormatter(markupText: textWithNewCarriageReturns)
format.perPageContentInsets.top = 72
format.perPageContentInsets.bottom = 72
format.perPageContentInsets.left = 72
format.perPageContentInsets.right = 72
printController.printFormatter = format
printController.present(animated: true, completionHandler: nil)
}