Skip to content

Instantly share code, notes, and snippets.

@stulevine
Created February 25, 2017 12:59
Show Gist options
  • Save stulevine/833da8127ae83eddcb47923ed66eec27 to your computer and use it in GitHub Desktop.
Save stulevine/833da8127ae83eddcb47923ed66eec27 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
class IndexCardImageView: UIImageView {
let topSpacing: CGFloat = 80.0
var lineColor = UIColor.clear
var lineWidth: CGFloat = 1.0
var topLineColor = UIColor.clear
var topLineWidth: CGFloat = 2.0
init(frame: CGRect,
lineSpacing: CGFloat = 35.0,
withLines: Bool = true,
backgroundColor: UIColor = UIColor(red: 0.9999, green: 0.9956, blue: 0.9749, alpha: 1.0),
lineColor: UIColor = UIColor(red: 0.3964, green: 0.6393, blue: 0.9988, alpha: 0.5),
topLineColor:UIColor = UIColor(red: 0.8338, green: 0.3722, blue: 0.3937, alpha: 0.5)) {
super.init(frame: frame)
self.backgroundColor = backgroundColor
self.topLineColor = topLineColor
self.lineColor = lineColor
self.image = self.drawIndexCard(frame, lineSpacing: lineSpacing, withLines: withLines)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func drawIndexCard(_ rect: CGRect, lineSpacing: CGFloat = 24.0, withLines: Bool = true) -> UIImage? {
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()!
// top red line
context.beginPath()
context.setStrokeColor(topLineColor.cgColor)
context.setLineWidth(topLineWidth)
context.move(to: CGPoint(x: 0.0, y: topSpacing))
context.addLine(to: CGPoint(x: rect.width, y: topSpacing))
context.strokePath()
// add blue lines if we want them
if withLines {
let deltaY: CGFloat = lineSpacing;
let numberOfLines: Int = Int((rect.height - topSpacing) / deltaY)
context.beginPath()
context.setStrokeColor(lineColor.cgColor)
context.setLineWidth(lineWidth)
for i in 1...numberOfLines {
let Y = CGFloat(i) * deltaY;
context.move(to: CGPoint(x: 0.0, y: topSpacing + Y))
context.addLine(to: CGPoint(x: rect.width, y: topSpacing + Y))
}
context.strokePath()
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
let windowFrame = CGRect(x: 0.0, y: 0.0, width: 375, height: 667)
let cardImageView = IndexCardImageView(frame: windowFrame)
PlaygroundPage.current.liveView = cardImageView
PlaygroundPage.current.needsIndefiniteExecution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment