Swift PDFKit 更改背景颜色

Nie*_*els 3 ios swift ios-pdfkit

我想在 Swift 中使用苹果 PDFKit 框架更改 pdf 的背景颜色,但它不起作用。创建一些控件(如文本或图像)然后使用它不是问题,但我想更改文档本身的颜色。有没有人有任何想法或解决方案?

  let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)

  let data = renderer.pdfData { (context) in

    context.beginPage()

    let attributes = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12),
      NSAttributedString.Key.backgroundColor : UIColor.green
    ]

    let text = "My pdf"

    text.draw(in: CGRect(x: 0, y: 0, width: 200, height: 20), withAttributes: attributes)

    //?
    UIColor.blue.setFill()
    UIColor.blue.setStroke()
    //?
    context.cgContext.setFillColor(cyan: 1.0, magenta: 1.0, yellow: 0.6, black: 1.0, alpha: 1.0)

  }

  return data
Run Code Online (Sandbox Code Playgroud)

var*_*i28 5

用这个

let currentContext = UIGraphicsGetCurrentContext()
currentContext?.setFillColor(UIColor.blue.cgColor)
currentContext?.fill(CGRect(x: x, y: y, width: Width, height: Height))
Run Code Online (Sandbox Code Playgroud)