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 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] [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.
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 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
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!
Thanks very much!
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)
Here is the Swift 4 code for printing text:
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)
}
Alternatively, if you have a long string with multiple \n carriage returns, you need to first replace all of the \n occurrences with br / before submitting the string to the UIMarkupTextPrintFormatter. Here is an example for 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)
}
来源:https://stackoverflow.com/questions/24963925/airprint-the-contents-of-a-text-view-using-swift