使用Swift快速打印文本视图的内容 [英] AirPrint the contents of a Text View using Swift
问题描述
我有一个UITextView,我需要使用Swift来AirPrint内容。我试图尽可能地适应Object-C代码,但是我不知道Object-C(或如何将其转换为Swift),因此这是一个很大的挑战。这是我到目前为止所拥有的:
I have a UITextView and I need to AirPrint the contents using Swift. I've tried to adapt Object-C code as much as possible, but I don't know Object-C (or how to convert it to Swift) so it has been quite a challenge. Here is what I have so far:
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的打印机。我点击了print,它与打印机通信,将其发送(告诉它)到打印机并退出...但是什么都没打印。
If I activate the "if" statement of course it fails and doesn't attempt to print. But if I comment it out as it is now and force the print attempt, it brings up the printer selection dialog where I choose my AirPrint enabled printer. I hit print and it communicates with the printer, sends it (so it says) to the printer and exits... but nothing prints.
如果有帮助的话,在这里而是打印机模拟器的结果:
If it helps any, here are the results from the printer simulator instead:
[25 / Jul / 2014:16:16:51 -0400] [客户端1]加密连接。
[25 / Jul / 2014:16:16:51 -0400] [客户端1]来自
[v1.fe80 :: 6676:baff:feb2:5e42 + en0]的连接现已加密。 p>
[25/Jul/2014:16:16:51 -0400] [Client 1] Encrypting connection. [25/Jul/2014:16:16:51 -0400] [Client 1] Connection from [v1.fe80::6676:baff:feb2:5e42+en0] now encrypted.
该输出在我选择打印机时发生,但在选择打印时不再显示。
That output happens when I choose the printer, but nothing more appears there when I choose print.
以下是一些其他信息,AppCode向我显示了这是否有帮助:
Here is some additional information AppCode shows me if this is of any help:
2014-07-25 16:16 :50.823 LotteryOddsBoost5 [2255:23613]-[PKPaperList
matchPaper:preferBorderless:withDuplexMode:didMatch:]
paperToMatch = result = matchType = 0
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
matchingPaper: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
matchPaper: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特定的文档和示例代码只是让我大吃一惊!
Any help would be GREATLY appreciated! I've worked through a ton of issues getting this program created, but the lack of Swift specific documentation and sample code are just kicking my butt on this!
非常感谢!
推荐答案
我认为您通过提供视图格式化程序和要打印的数据而感到困惑。尝试:
I think you're confusing it by offering both the view formatter and data to print. Try:
var pic:UIPrintInteractionController = .sharedPrintController()
var viewpf:UIViewPrintFormatter = myTextView.viewPrintFormatter()
pic.delegate = self
pic.showsPageRange = true
pic.printFormatter = viewpf
pic.presentAnimated(true, completionHandler: nil)
这篇关于使用Swift快速打印文本视图的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!