【问题标题】:Best way to print a NSTableView - Swift 5 + cocoa打印 NSTableView 的最佳方式 - Swift 5 + cocoa
【发布时间】:2022-03-25 03:03:03
【问题描述】:

我是 Swift 可可编程的新手,我正在尝试构建一个金融程序。我几乎完成了,但我遇到了以下问题。我正在尝试从带有 textlabel 前后的情节提要打印多页 NStableView。如果 tableView 适合一页,我可以毫无问题地打印 NSView。如果超过一页,则仅显示第一页。我没有找到如何扩展 Tableview。

我在没有有效解决方案的情况下搜索了所有帖子。

我可以在没有 NSView 的情况下打印 NSTableView 本身,它将显示所有页面。但当然,它不会打印伴随的文本标签,也不会打印 TableheaderView。

我设法调整了一个示例,让我打印带有标题的整个表格,但没有文本标签。它逐个单元格地重建表格以进行打印。

我还找到了一个更有希望的示例,我可以在其中使用 stackView 打印所有我需要的内容。

2个问题:

  1. stackView 内的 tableView 打印正常,但它想要调整大小或剪切最后一列。我不得不部分成功地使用帧大小和其他选项。
  2. 我曾尝试在其 stackView 中单独打印 TableHeaderView,但效果不佳。因此,我在情节提要中绘制了与 tableView 标题匹配的水平 StackView 中的一系列标签。情节提要中的一切看起来都很好,但程序会在打印前调整每个标签的大小。找不到解决问题的方法。

    任何建议将不胜感激。 SwiftUI 更容易使用吗?我必须将我的操作系统更新到 10.15...

  • 您所说的“前后带有文本标签”是什么意思?第一页顶部和最后一页底部的标签?
  • 可以关闭表格列的自动调整大小。
  • 是的,我在表格前后都有文本标签。我刚刚发现在情节提要中有问题的元素上添加宽度约束解决了这个问题。所以基本上,我可以用 stackView 打印我想要的东西。不过,我认为我可以只打印 NSView 并展开 NSTableView 以显示所有行。
  • 调整视图大小以展开表格视图。堆栈视图是否会自动扩展和调整大小?在多页上打印表格视图并在每页上打印其标题需要一些复杂的代码。支持缩放、页面大小、方向和其他设置增加了复杂性。
  • 调整 NSView 的大小不会扩展 TableView。我已经读过停用 scrollView 可能会完成这项工作,但我没有成功。单独打印 NSTableView 或在 stackView 中工作正常,但 HeaderView

标签: swift cocoa printing nstableview nsstackview


【解决方案1】:

我现在找到的解决方案是在 Stackview 中构建我的打印输出,但必须有更好的解决方案。 Stackview 中的 TableView 打印正常,但 HeaderView 不打印。我使用的解决方案是用一系列标签重建一个 HeaderView 并将它们放在一个水平 StackView 中,我可以在表格之前的垂直 Stackview 中打印。标题是在情节提要中设计的。注意打印参数并没有完全优化,我只是从Xcode开始。the printout include a Title, the headerView, the TableView and a Label with the Inventory total. The table can be multipage. The content of the table is fictive]1

let printOpts: [NSPrintInfo.AttributeKey: Any] = [.headerAndFooter: true, .orientation : 1]
let printInfo = NSPrintInfo(dictionary: printOpts)

//printInfo.paperSize = NSMakeSize(595, 842)

printInfo.paperSize = NSMakeSize(612, 792)

printInfo.scalingFactor = 0.8
printInfo.orientation = .landscape

printInfo.leftMargin = 30
printInfo.rightMargin = 0
printInfo.topMargin = 40
printInfo.bottomMargin = 30


let titreLabel = NSTextField.init(labelWithString: monTitre)
let InValueText =  valeur_inv.stringValue  //  "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
let InvValuesLabel = NSTextField.init(wrappingLabelWithString: "Valeur d'inventaire: " +  InValueText + " $")


let pageContentSize = NSSize(width: printInfo.paperSize.width - printInfo.leftMargin - printInfo.rightMargin,
height: printInfo.paperSize.height - printInfo.topMargin - printInfo.bottomMargin)
let initialFrameForPrinting = NSRect(origin: .zero, size: pageContentSize)

let stackView = NSStackView(frame: initialFrameForPrinting)
stackView.autoresizesSubviews = false
stackView.autoresizingMask = [.height]
stackView.orientation = .vertical
stackView.alignment = .left

stackView.addArrangedSubview(titreLabel)
stackView.spacing = 20.0
stackView.addArrangedSubview(header_stackView)  // an horizontal stackview with the header, build in the storyboard.
stackView.spacing = 0   // -20 does not work
stackView.addArrangedSubview(PrintTableView)
//PrintTableView.sizeLastColumnToFit()
stackView.spacing = 20
stackView.addArrangedSubview(InvValuesLabel)  // Inventory values at the bottom of the table

//stackView.addArrangedSubview(ValInvBoxView)

  // won't work earlier than when the table is embedded in a view hierarchy

 // MARK: Configure the print operation
 // Print 'naturally', starting in top-left (for LTR languages at least?) instead of centering the content like a picture.
 printInfo.isHorizontallyCentered = true
 printInfo.isVerticallyCentered = false

 // Using `.fit` would shrink the content if it was a single-line label, but actually behaves the same with our more complex layout.
 //printInfo.horizontalPagination = .clip
 printInfo.horizontalPagination = .fit
 printInfo.verticalPagination = .automatic
 printInfo.scalingFactor = 0.8

 let printOperation = NSPrintOperation(view: stackView, printInfo: printInfo)
// let printOperation = NSPrintOperation(view: myPrintView, printInfo: printInfo) // print the table header only

 printOperation.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
 printOperation.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
//printOperation.showsPrintPanel = true
 printOperation.showsProgressPanel = true
 
 printOperation.run()
 printOperation.cleanUp()

self.dismiss(true) in the storyboard.  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    相关资源
    最近更新 更多