SwiftyRuler is a very simple Swift package that implements an accurate ruler for any iOS device. Nothing more, nothing less.
To run the example project, clone the repo, and run pod install
from the Example directory first.
Vertical | Horizontal | Document |
---|---|---|
To integrate using Apple's Swift Package Manager, add the following as a dependency to your Package.swift
:
dependencies: [
.package(url: "https://github.com/fatihbalsoy/SwiftyRuler.git", from: "1.0.0")
]
Alternatively, navigate to your Xcode project, go to File > Add Packages...
and search for https://github.com/fatihbalsoy/SwiftyRuler
.
If you prefer not to use any of the aforementioned dependency managers, you can integrate SwiftyRuler into your project manually. Simply drag the files in the Sources
folder into your Xcode project.
import SwiftyRuler
import UIKit
class ViewController : UIViewController, RulerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let ruler = Ruler()
ruler.delegate = self
// Tick Lengths //
ruler.longTickLength = 30
ruler.shortTickLength = 10
ruler.midTickLength = ruler.midLineLength(2)
// Properties //
ruler.pixelAccurate = true
ruler.doubleUnits = true
ruler.direction = .horizontal
// ruler.doubleSided = false
// Colors //
ruler.backgroundColor = UIColor.label.withAlphaComponent(0.05)
ruler.tickColor = .label
ruler.labelColor = .label
// Set Pixel Density //
// ~ Useful for macOS and tvOS ~ //
ruler.setPixelDensity(218.0)
view.addSubview(ruler)
// Setup Constraints //
}
}
@objc optional func ruler(didPressAccuracyWarning ruler: Ruler, using event: UIEvent)
Triggered when accuracy disclaimer is pressed. Can be used to implement a user interface claiming the inaccuracy of the ruler and giving the option to enter a custom pixel density.
@objc optional func ruler(didPressCustomizePixelDensity ruler: Ruler, currentDensity: CGFloat, using event: UIEvent)
Triggered when the edit button is pressed. Can be used to implement a user interface to adjust the pixel density of the ruler.
- GBDeviceInfo
- Used to fetch PPI (Pixels per Inch) of iOS displays.
SwiftyRuler is available under the AGPL license. See the LICENSE file for more info.