如何在 iOS PDFKit 中正确传递数组

How can I properly pass an array in iOS PDFKit

我正在尝试在 [string] 数组中获取 PDF。以下是我到目前为止所拥有的。我想我需要在某个地方做一个 foreach,但我不完全确定。

我认为这样的事情可能行得通,但行不通。

       for entry in body {
        let attributedText = NSAttributedString(
            string: entry,
            attributes: textAttributes
        )
    }

private func addBody(body: [String], pageRect: CGRect, textTop: CGFloat) {
    let pageWidth = 8.5 * 72.0
    let pageHeight = 11 * 72.0
    let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

    let bodyCG = addInstructor(instructor: "", pageRect: pageRect)
    let textFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .natural
    paragraphStyle.lineBreakMode = .byWordWrapping

    let textAttributes = [
      NSAttributedString.Key.paragraphStyle: paragraphStyle,
      NSAttributedString.Key.font: textFont
    ]

    let attributedText = NSAttributedString(
        string: body,
      attributes: textAttributes
    )

    let textRect = CGRect(
      x: 15,
      y: bodyCG + 30,
      width: pageRect.width - 20,
      height: pageRect.height - textTop - pageRect.height / 5.0
    )
    attributedText.draw(in: textRect)
}

添加一些额外的细节。如果我不使用 [String] 而只使用 String 一切正常。 PDF 已生成,我很难理解的概念是如何为 PDF 传递数组。

var courseAttendees : [String] = ["name", "name", "name", "name"]

例如,我想传递 courseAttendees 然后遍历数组,名称只是重叠并显示在下面。

最终代码。

private func addBody(body: [String], textTop: CGFloat) {
        let pageWidth = 8.5 * 72.0
        let pageHeight = 11 * 72.0
        let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

        let bodyCG = addInstructor(instructor: "", pageRect: pageRect)
        let textFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)

        let textAttributes: [NSAttributedString.Key: Any] =
          [NSAttributedString.Key.font: textFont]

        // keep track of the y position on the page. You might need
        // to set this globally as you have multiple pages
        var currentYPos: CGFloat = bodyCG

        // Loop through the array
         for entry in body {

             let attributedText = NSAttributedString(
                string: "\(entry)",
               attributes: textAttributes
             )

             // Update the currentYPos
             currentYPos += 15

             // Use the currentYPos in the textRect
             let textRect = CGRect(
               x: 15,
               y: currentYPos,
               width: pageRect.width - 20,
               height: pageRect.height - textTop - pageRect.height / 5.0
             )
             attributedText.draw(in: textRect)
         }
    }

根据您的问题和图片,我假设 PDF 创建工作正常,但数据未按预期呈现。

我认为这里要回答的两个问题是:

  1. 在哪里循环你的数组
  2. 如何跟踪负责垂直定位的页面中的当前y坐标

以下是我为尝试解决您的问题所做的一些补充,我已对更改的内容添加评论

// Somewhere appropriate in your code
var courseAttendees : [String] = ["name1", "name2", "name3", "name4"]

// Call the function, 15 is just a random number for textTop, 
// give it what you feel is appropriate
addBody(body: courseAttendees, textTop: 15)

// I have removed the pageRect parameter since you create it
// in the function
private func addBody(body: [String], textTop: CGFloat) {
    let pageWidth = 8.5 * 72.0
    let pageHeight = 11 * 72.0
    let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

    let bodyCG = addInstructor(instructor: "", pageRect: pageRect)
    let textFont = UIFont.systemFont(ofSize: 12.0, weight: .regular)

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .natural
    paragraphStyle.lineBreakMode = .byWordWrapping

    let textAttributes = [
      NSAttributedString.Key.paragraphStyle: paragraphStyle,
      NSAttributedString.Key.font: textFont
    ]
    
    // keep track of the y position on the page. You might need
    // to set this globally as you have multiple pages
    var currentYPos: CGFloat = 0.0
    
    // Loop through the array
    for entry in body {
        
        let attributedText = NSAttributedString(
            string: entry,
          attributes: textAttributes
        )
        
        // Update the currentYPos
        currentYPos += bodyCG + 30

        // Use the currentYPos in the textRect
        let textRect = CGRect(
          x: 15,
          y: currentYPos,
          width: pageRect.width - 20,
          height: pageRect.height - textTop - pageRect.height / 5.0
        )
        attributedText.draw(in: textRect)
    }
}

以上内容我还没有测试过,所以请试一试,看看它是否能解决您的问题。

如果没有,请发表评论,我会相应地进行更新。