【问题标题】:Swift PDFKit change background colorSwift PDFKit 更改背景颜色
【发布时间】:2020-08-29 15:21:20
【问题描述】:

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

【问题讨论】:

  • 解决了吗?

标签: ios swift ios-pdfkit


【解决方案1】:
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(currentContext, [UIColor blueColor].CGColor );
CGContextFillRect(currentContext, CGRectMake(0, 110.5, pageSize.width, pageSize.height));

【讨论】:

    【解决方案2】:

    使用这个

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

    【讨论】:

      猜你喜欢
      • 2022-06-16
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 2015-10-09
      • 2013-08-08
      相关资源
      最近更新 更多