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

时间:2016-09-22 23:20:29

标签: ios swift printing line-breaks

在我的应用程序中,我将一个UITextView实例和一个UIButton实例拖到我的View Controller上。

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

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

我的全班看起来像这样:

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中,当我这样做时,会在那里保留换行符。只有当我发送到打印机时才会被删除。

任何帮助都将不胜感激。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

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