Reputation: 11
I am a beginner with xcode and swift.
I have a couple of fields and a image in my viewcontroller and I would like to print the content of this fields...
My code from a tutorial:
@IBAction func print(sender: AnyObject) {
// 1
let printController = UIPrintInteractionController.sharedPrintController()
// 2
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.General
printInfo.jobName = "Rapport"
printController.printInfo = printInfo
// 3
let formatter = UIMarkupTextPrintFormatter(markupText: itemName.text!)
formatter.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
printController.printFormatter = formatter
// 4
printController.presentAnimated(true, completionHandler: nil)
}
works very well with this only textfield. But how can I print the rest of it?
Upvotes: 0
Views: 290
Reputation: 5479
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)
}
Upvotes: 0
Reputation: 530
You are using a formatter to send to the printController
and send him a markup text to print. This function takes a string, so you can create a custom string with all the text you want it to have and call the function, like this:
// 3
let myMarkupText = itemName.text! + "\n" + secondField.text! + "\n" + anotherField.text!
let formatter = UIMarkupTextPrintFormatter(markupText: myMarkupText)
...
I added the "\n" to start a new line, but you can format it the way you want, of course. I'm not sure if "\n" would create a new line anyway (since this is markup, maybe you need <br />
), you'd have to try and see.
If you wanted to print the whole page instead of just certain parts, you could also check the documentation for UIPrintInteractionController to see what other options you have (printingItem
, printingItems
, printPageRenderer
).
Upvotes: 0