0

I'm trying to have a heading where the whole top of the PDF document is filled with a color.

The standard behaviour seem to be no matter how big I set the CGRect to clip to the edges of the text.

let attributes = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
      NSAttributedString.Key.backgroundColor : UIColor.red,
      NSAttributedString.Key.foregroundColor : UIColor.white
    ] 

let text = "Hello World!"
    text.draw(in: CGRect(x: 0, y: 10, width: 2000, height: 2000), withAttributes: attributes)

See image attached.

I have done a dodgy solution but not happy with it as the user can copy the 'spaces' rather than the actual test. Really not an elegant solution:

let attributes = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
      NSAttributedString.Key.backgroundColor : UIColor.red,
      NSAttributedString.Key.foregroundColor : UIColor.white
    ]
    
    let attributesBack = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 32),
      NSAttributedString.Key.backgroundColor : UIColor.red,
      NSAttributedString.Key.foregroundColor : UIColor.white
    ]
    
   
    let line = "                                                     "
    let text = "Hello World!"
    line.draw(in: CGRect(x: 0, y: 0, width: 2000, height: 2000), withAttributes: attributesBack)
    text.draw(in: CGRect(x: 0, y: 10, width: 2000, height: 2000), withAttributes: attributes)

Is Thera a way: a. To either set the CGRect the text is drawn in to spread across the whole page, like a Spacer()? b. A way to draw an actual rectangle and then draw the text on-top of that?

Thank youenter image description here

Etienne
  • 89
  • 4

1 Answers1

0

Found the following solution. using UIRectFill.

func drawHeader(_ drawContext: CGContext, pageRect: CGRect) {
  
  drawContext.saveGState()
  drawContext.setLineWidth(20.0)
    
    drawContext.addRect(CGRect(x: 0, y: 0, width: pageRect.width, height: pageRect.height))
    drawContext.setFillColor(red: 177.0/256.0, green: 205.0/256.0, blue: 220.0/256.0, alpha: 0.5)
    UIRectFill(CGRect(x: 0, y: 0, width: pageRect.width, height: pageRect.height))
  drawContext.saveGState()
  drawContext.restoreGState()
}


func myPDF() -> Data {
  let pdfMetaData = [
    kCGPDFContextCreator: "my PDF",
    kCGPDFContextAuthor: "etienne"
  ]
  let format = UIGraphicsPDFRendererFormat()
  format.documentInfo = pdfMetaData as [String: Any]
    let pageWidth = 595.2 //8.5 * 72.0
  let pageHeight = 841.8 //11 * 72.0
  let pageRect = CGRect(x: 0, y: 0, width: pageWidth, height: pageHeight)

  let renderer = UIGraphicsPDFRenderer(bounds: pageRect, format: format)
  
  let data = renderer.pdfData { (context) in
  
    context.beginPage()
   
    let attributes = [
      NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16),
      NSAttributedString.Key.backgroundColor : UIColor.red,
      NSAttributedString.Key.foregroundColor : UIColor.blue
    ]
    
    let text = "Hello World!"
    
    text.draw(in: CGRect(x: 0, y: 10, width: 2000, height: 2000), withAttributes: attributes)
    
    
    let context = context.cgContext
    drawHeader(context, pageRect: pageRect)
    
  }

  return data
}
Etienne
  • 89
  • 4