1

我正在尝试在一个[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)
         }
    }
4

1 回答 1

0

根据您的问题和图像,我假设 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)
    }
}

我没有测试过上面的,所以请试一试,看看它是否能解决你的问题。

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

于 2022-01-18T04:17:59.120 回答