Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Draws a `singlePixelLine` line above or below the given point, depending on `topBias`.
import UIKit
func singlePixelLine(at y: CGFloat, in rect: CGRect, topBias: Bool = true) {
let offset = pixelUnit/2.0
let adjustedY = round(from: y - offset, fraction: pixelUnit, down: topBias) + offset
let line = makeLine(at: adjustedY, in: rect)
strokePath(line, width: pixelUnit)
var pixelUnit: CGFloat {
return 1.0 / UIScreen.main().scale
func round(from: CGFloat, fraction: CGFloat, down: Bool = true) -> CGFloat {
let expanded = from / fraction
let rounded = (down ? floor : ceil)(expanded)
return rounded * fraction
func makeLine(at y: CGFloat, in rect: CGRect) -> UIBezierPath {
let line = UIBezierPath()
line.move(to: CGPoint(x: rect.minX, y: y))
line.addLine(to: CGPoint(x: rect.maxX, y: y))
return line
func strokePath(_ path: UIBezierPath, width: CGFloat) {
path.lineWidth = width
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment