InkKit
Swift Support
Swift 4.0
InkKit is Swift 4.0 by default, so to use that just include InkKit in your podfile:
pod 'InkKit'
Swift 3.2
In order to use InkKit in a Swift 3.2/3.0 project, ensure you point to the 2.1.0
tag.
pod 'InkKit', '2.1.0'
Swift 2.3
In order to use InkKit in a Swift 2.3 project, ensure you point to the swift2.3
branch.
pod 'InkKit', :git => 'https://github.com/shaps80/InkKit/', :branch => 'swift2.3'
Swift 2.2
If you still require Swift 2.2, point your version as such:
pod 'InkKit', '1.3.1'
Example Code
Everything you see here, was code-drawn with InkKit! In fact, other than some CGRect
instances, this is ALL the code required to draw the image you see on the right ;)
Change Log
v2.0.0
Note: Since this is a Swift 3 release, I decided to also clean up the API and remove all deprecation warnings. InkKit 2.0 should be considered a new API.
- Swift 3.0 Support
- Updated API to support Swift 3.0 guidelines
- Shear Transforms
- Perspective Transforms
- Radians from Degrees function (and inverse)
- Corner Radius with Concave, Convex and Line support
- New Color value-type
v1.3.1
- OSX Support
- OSX Demo Project now included
Tablerenamed to GridTablerenamed to GridComponents- Added convenience methods for working with paths
v1.2.0
- Shadows
- Borders
- Tables
v1.1.0
- Images
- Strings
v1.0
- Fills
- Strokes
- Geometry
API
InkKit provides many useful convenience methods for drawing and geometry calculations. Its also cross platform working across iOS & MacOS
Core
If the convenience methods below don't solve your needs, you can start by using the new methods added directly to CGContext
itself:
func draw(inRect:attributes:drawing:)
Which would look like this in usage:
CGContext.current?.draw(inRect: rect, drawing: { (context, rect, attributes) in
Color.redColor.setFill()
UIRectFill(rect)
})
This basically wraps getting the context, setting up its frame and save/restore calls. If you provide the additional DrawingAttributes block, it will also pre-configure your context with those options for you.
Grid
init(colCount:rowCount:bounds:)
func positionForCell(atIndex:) -> (col: Int, row: Int)
func boundsForCell(atIndex:) -> CGRect
func boundsForRange(sourceColumn:sourceRow:destinationColumn:destinationRow:) -> CGRect
func boundsForCell(col:row:) -> CGRect
func enumerateCells(enumerator:(index:col:row:bounds:) -> Void)
A Grid
is a really great way for laying out your drawing without having to think about placement, rect translations, etc...
I use them often for layout only, but sometimes its useful to be able to render them as well (like in the included demo).
// components is a bitmask [ .Outline, .Rows, .Columns ]
func stroke(components:attributes:)
Borders & Shadows
Supports .Outer
, .Inner
and .Center
borders, as well as .Outer
and .Inner
shadows.
static func addBorder(type:path:attributes:)
static func addShadow(type:path:color:radius:offset:)
Strokes
static func strokeLine(startPoint:endPoint:startColor:endColor:angleInDegrees:attributes:)
static func strokeLine(startPoint:endPoint:color:attributes:)
static func strokePath(path:startColor:endColor:angleInDegrees:attributes:)
Fills
static func fillPath(path:startColor:endColor:angleInDegrees:attributes:)
Geometry
Many of the drawing methods use the geometry additions below, but they can also be useful for your own projects:
func divide(atDelta:fromEdge:margin:) -> (slice, remainder)
func insetBy(edgeInsets:) -> CGRect
mutating func insetInPlace(edgeInsets:)
func alignedTo(rect:horizontal:vertical:) -> CGRect
func scaledTo(rect:scaleMode:) -> CGRect
func gradientPoints(forAngleInDegrees:) -> (start, end)
func scaledTo(size:scaleMode:) -> CGSize
func reversibleRect(fromPoint:toPoint:) -> CGRect
Images
There are also additional draw methods for images:
func drawAlignedTo(rect:horizontal:vertical:blendMode:alpha:)
func drawScaledTo(rect:scaleMode:blendMode:alpha:)
static func circle(radius:attributes:) -> Image
static func draw(width:height:scale:attributes:drawing:) -> Image
static func draw(size:scale:attributes:drawing:) -> Image
Strings
Finally, we even have some easy draw methods for strings:
func drawAlignedTo(rect:horizontal:vertical:attributes:constrainedSize:)
func sizeWithAttributes(attributes:constrainedSize:) -> CGSize
func drawAtPoint(point:attributes:)
func drawInRect(rect:withAttributes)
Demo
To try it out yourself, download the source and run the included demo project.
Platforms and Versions
InkKit is supported on the following platforms:
- iOS 8.0 and greater
- OSX 10.11 and greater
Installation
InkKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'InkKit'
Alternatively you can simply drag the files into your iOS or OSX project.
Author
License
InkKit is available under the MIT license. See the LICENSE file for more info.