A thin wrapper around Core Graphics that makes it possible to reuse the same drawing code on both iOS and macOS.
- Add the following to your Cartfile:
github "kaishin/Thyme"
- Then run
carthage update
- Follow the current instructions in Carthage's README for up to date installation instructions.
- Add the following to your Podfile:
pod 'Thyme'
- You will also need to make sure you're opting into using frameworks:
use_frameworks!
- Then run
pod install
.
After importing Thyme, you can use the same APIs on both iOS and OSX. The exact same drawing code can be used in both platforms.
Thyme introduces the Path
type to help you contruct your paths. Instead of providing exact coordinates, you describe how your path can be drawn in a series of actions. For instance:
let square = Path(point: CGPointZero)
.addLine(towards: [.Right: 100])
.addLine(towards: [.Bottom: 100])
.addLine(towards: [.Left: 100])
.addLine(towards: [.Top: 100])
.close()
The code snippet above describes a square-shaped path in 5 steps. You can then obtain a CGPathRef
using the CGPath
property of Path
.
This is a simplified example to demonstrate how you can use the same drawing code on both platforms, with the use of helpers such as CGRect.topLeftPoint
:
// Shared Code
func drawIcon(context: CGContextRef, rect: CGRect, fillColor: CGColorRef) {
let triangle = Path(point: rect.topLeftPoint)
.addLine(to: rect.topRightPoint)
.addLine(towards: [.left: rect.width / 2, .bottom: rect.height])
.close()
context.addPath(triangle.cgPath)
context.setFillColor(fillColor)
context.fillPath()
}
// iOS
class CustomView: UIView {
override func drawRect(rect: CGRect) {
drawInCurrentContext { context in
drawIcon(context, rect: self.bounds, fillColor: UIColor.redColor())
}
}
}
// Mac
class CustomView: NSView {
override func drawRect(dirtyRect: NSRect) {
drawInCurrentContext { context in
drawIcon(context, rect: self.bounds, fillColor: NSColor.redColor())
}
}
}
- Swift 4.0+
- Xcode 9
- iOS 10+
- macOS 10.10+
Copyright 2015-2018 Reda Lemeden. BSD Licence. See LICENSE file for more info.