Created
April 28, 2021 21:40
-
-
Save DonMag/dddae6e14563de97455b956bc5f5defc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generate a PDF with | |
// a 6-inch vertical line crossed by a 6-inch horizontal line | |
// vertical and horizontal "1-inch hash" lines | |
// 6-, 4- and 2-inch diameter circles | |
// see app image here: https://imgur.com/a/O9dmB9M | |
import PDFKit | |
class ViewController: UIViewController { | |
let pdfView = PDFView() | |
let createButton = UIButton() | |
let shareButton = UIButton() | |
var pdfData: Data? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .systemYellow | |
[pdfView, createButton, shareButton].forEach { | |
$0.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview($0) | |
} | |
let g = view.safeAreaLayoutGuide | |
NSLayoutConstraint.activate([ | |
pdfView.heightAnchor.constraint(equalTo: g.heightAnchor, multiplier: 0.6), | |
pdfView.widthAnchor.constraint(equalTo: pdfView.heightAnchor, multiplier: 8.5 / 11.0), | |
pdfView.centerXAnchor.constraint(equalTo: g.centerXAnchor), | |
pdfView.centerYAnchor.constraint(equalTo: g.centerYAnchor), | |
createButton.bottomAnchor.constraint(equalTo: pdfView.topAnchor, constant: -20.0), | |
createButton.leadingAnchor.constraint(equalTo: pdfView.leadingAnchor), | |
createButton.trailingAnchor.constraint(equalTo: pdfView.trailingAnchor), | |
shareButton.topAnchor.constraint(equalTo: pdfView.bottomAnchor, constant: 20.0), | |
shareButton.leadingAnchor.constraint(equalTo: pdfView.leadingAnchor), | |
shareButton.trailingAnchor.constraint(equalTo: pdfView.trailingAnchor), | |
]) | |
createButton.backgroundColor = .blue | |
createButton.setTitle("Create", for: []) | |
createButton.setTitleColor(.white, for: .normal) | |
createButton.setTitleColor(.gray, for: .highlighted) | |
shareButton.backgroundColor = .red | |
shareButton.setTitle("Share", for: []) | |
shareButton.setTitleColor(.white, for: .normal) | |
shareButton.setTitleColor(.gray, for: .highlighted) | |
pdfView.autoScales = true | |
createButton.addTarget(self, action: #selector(createTap(_:)), for: .touchUpInside) | |
shareButton.addTarget(self, action: #selector(shareTap(_:)), for: .touchUpInside) | |
} | |
@objc func shareTap(_ sender: Any?) -> Void { | |
if let data = pdfData { | |
let vc = UIActivityViewController(activityItems: [data], applicationActivities: []) | |
present(vc, animated: true, completion: nil) | |
} | |
} | |
@objc func createTap(_ sender: Any?) -> Void { | |
pdfData = createTest() | |
if let data = pdfData { | |
pdfView.document = PDFDocument(data: data) | |
} | |
} | |
func createTest() -> Data { | |
let format = UIGraphicsPDFRendererFormat() | |
let pageWidth = 8.5 * 72.0 | |
let pageHeight = 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 context = context.cgContext | |
drawMajorLines(context, pageRect: pageRect) | |
drawHashLines(context, pageRect: pageRect) | |
drawCircles(context, pageRect: pageRect) | |
} | |
return data | |
} | |
func drawMajorLines(_ drawContext: CGContext, pageRect: CGRect) -> Void { | |
let r = CGRect(x: pageRect.midX - (72.0 * 3.0), | |
y: pageRect.midY - (72.0 * 3.0), | |
width: 72.0 * 6.0, | |
height: 72.0 * 6.0) | |
drawContext.saveGState() | |
drawContext.move(to: CGPoint(x: r.minX, y: r.midY)) | |
drawContext.addLine(to: CGPoint(x: r.maxX, y: r.midY)) | |
drawContext.move(to: CGPoint(x: r.midX, y: r.minY)) | |
drawContext.addLine(to: CGPoint(x: r.midX, y: r.maxY)) | |
drawContext.setStrokeColor(UIColor.black.cgColor) | |
drawContext.strokePath() | |
drawContext.restoreGState() | |
} | |
func drawHashLines(_ drawContext: CGContext, pageRect: CGRect) -> Void { | |
let r = CGRect(x: pageRect.midX - (72.0 * 3.0), | |
y: pageRect.midY - (72.0 * 3.0), | |
width: 72.0 * 6.0, | |
height: 72.0 * 6.0) | |
drawContext.saveGState() | |
var y: CGFloat = r.midY - 72.0 * 0.5 | |
var x: CGFloat = r.minX | |
for i in 1...7 { | |
if i != 4 { | |
drawContext.move(to: CGPoint(x: x, y: y)) | |
drawContext.addLine(to: CGPoint(x: x, y: y + 72.0)) | |
} | |
x += 72.0 | |
} | |
y = r.minY | |
x = r.midX - 72.0 * 0.5 | |
for i in 1...7 { | |
if i != 4 { | |
drawContext.move(to: CGPoint(x: x, y: y)) | |
drawContext.addLine(to: CGPoint(x: x + 72.0, y: y)) | |
} | |
y += 72.0 | |
} | |
drawContext.setStrokeColor(UIColor.blue.cgColor) | |
drawContext.strokePath() | |
drawContext.restoreGState() | |
} | |
func drawCircles(_ drawContext: CGContext, pageRect: CGRect) -> Void { | |
var r = CGRect(x: pageRect.midX - (72.0 * 3.0), | |
y: pageRect.midY - (72.0 * 3.0), | |
width: 72.0 * 6.0, | |
height: 72.0 * 6.0) | |
drawContext.saveGState() | |
drawContext.addEllipse(in: r) | |
r = r.insetBy(dx: 72.0, dy: 72.0) | |
drawContext.addEllipse(in: r) | |
r = r.insetBy(dx: 72.0, dy: 72.0) | |
drawContext.addEllipse(in: r) | |
drawContext.setStrokeColor(UIColor.red.cgColor) | |
drawContext.strokePath() | |
drawContext.restoreGState() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment