3

我想在 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
4

2 回答 2

5

用这个

let currentContext = UIGraphicsGetCurrentContext()
currentContext?.setFillColor(UIColor.blue.cgColor)
currentContext?.fill(CGRect(x: x, y: y, width: Width, height: Height))
于 2020-05-13T13:07:25.907 回答
2
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(currentContext, [UIColor blueColor].CGColor );
CGContextFillRect(currentContext, CGRectMake(0, 110.5, pageSize.width, pageSize.height));
于 2020-05-13T13:08:23.150 回答