打印 UITextView(使用蓝牙打印机)不保留换行符

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

在我的应用程序中,我将一个 UITextView 实例和一个 UIButton 实例拖到我的视图控制器上。

只要按下按钮,它就会获取 TextView 中的文本,并将数据发送到打印机进行打印。

在我的 ViewDidLoad() 方法中,我正在编译一个字符串并将其加载到我的 TextView 中。

我的完整 class 看起来像这样:

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()
    }
}

打印功能工作得很好,除了它全部打印在一行上,而不是保留我在准备好的字符串中包含的 \n 换行符。

如果有人想知道,我已经尝试将结果显示到 NSLog 中,当我这样做时,换行符会保留在那里。只有当我发送到打印机时,它们才会被删除。

如有任何帮助,我们将不胜感激。

提前致谢!

为了解决这个问题,我简单地做了一个字符串函数,将任何换行符 (\n) 替换为 HTML 换行符 (<br/>)