Printing a UITextView (with a bluetooth printer) does not preserve line breaks

陌路散爱 提交于 2021-01-07 04:53:29

问题


In my app, I have a UITextView instance and a UIButton instance dragged onto my View Controller.

Whenever the Button is pressed, it takes the text in the TextView, and sends the data to a printer to be printed.

In my ViewDidLoad() method, I am compiling a string and loading that into my TextView.

My full class looks like this:

class CSTSummaryViewController: UIViewController {

    @IBOutlet weak var summaryReport: UITextView!

    @IBAction func Print(sender: AnyObject) {
        let printController = UIPrintInteractionController.sharedPrintController()
        let printInfo = UIPrintInfo(dictionary: nil)
        printInfo.outputType = UIPrintInfoOutputType.General
        printInfo.jobName = "Print Summary"
        printController.printInfo = printInfo

        let format = UIMarkupTextPrintFormatter(markupText: summaryReport.text)
        format.contentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
        printController.printFormatter = format
        printController.presentAnimated(true, completionHandler: nil)
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        let reportString: String = "Property Owner: John Smith\n\nProperty ID: Main Street Residence\n\nReviewed By: Joe Schmoe\n\nNotes: These are some notes.\n\n"

        summaryReport.text = reportString
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

The print function works just fine, except for the fact that it all prints out on one single line, rather than preserving the \n newline characters that I have included in my prepared string.

In case anybody is wondering, I have tried displaying the results into NSLog, and when I do, the line breaks ARE preserved there. Only when I send to a printer do they get removed.

Any help would be appreciated.

Thank you in advance!


回答1:


To solve this issue, I simply did a string function that replaced any newline character (\n) to an HTML line break (<br/>)



来源:https://stackoverflow.com/questions/39650283/printing-a-uitextview-with-a-bluetooth-printer-does-not-preserve-line-breaks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!