打印UITextView(使用蓝牙打印机)不会保留换行符 [英] Printing a UITextView (with a bluetooth printer) does not preserve line breaks
问题描述
在我的应用中,我有一个UITextView实例和一个UIButton实例拖到我的View Controller中.
In my app, I have a UITextView instance and a UIButton instance dragged onto my View Controller.
每当按下按钮时,它都会在TextView中获取文本,并将数据发送到打印机进行打印.
Whenever the Button is pressed, it takes the text in the TextView, and sends the data to a printer to be printed.
在ViewDidLoad()方法中,我正在编译一个字符串并将其加载到TextView中.
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()
}
}
除了所有功能都打印在一行上,而不是保留我准备的字符串中包含的\ n换行符之外,打印功能可以正常工作.
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.
万一有人想知道,我尝试将结果显示到NSLog中,并且当我这样做时,将换行符保留在那里.只有当我发送到打印机时,它们才会被删除.
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.
任何帮助将不胜感激.
提前谢谢!
推荐答案
要解决此问题,我只是做了一个字符串函数,将所有换行符( \ n
)替换为HTML换行符(< br/>
)
To solve this issue, I simply did a string function that replaced any newline character (\n
) to an HTML line break (<br/>
)
这篇关于打印UITextView(使用蓝牙打印机)不会保留换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!