在我的应用程序中,我有一个 UITextView 实例和一个 UIButton 实例拖到我的 View Controller 上。
每当按下按钮时,它都会获取 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/>
)
关于ios - 打印 UITextView(使用蓝牙打印机)不会保留换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650283/