Latest 2.0
Homepage https://github.com/kaishin/Thyme
License BSD
Platforms ios 10.0, osx 10.10, requires ARC
Authors

logo

GitHub release Carthage compatible CocoaPods Swift 4.0 platforms

A thin wrapper around Core Graphics that makes it possible to reuse the same drawing code on both iOS and macOS.

Installation

Carthage

  • 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.

CocoaPods

  • 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.

Usage

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())
    }
  }
}

Requirements

  • Swift 4.0+
  • Xcode 9
  • iOS 10+
  • macOS 10.10+

License

Copyright 2015-2018 Reda Lemeden. BSD Licence. See LICENSE file for more info.

Latest podspec

{
    "name": "Thyme",
    "version": "2.0",
    "summary": "A thin wrapper around *Core Graphics* that makes it possible to reuse the same drawing code on both iOS and macOS.",
    "homepage": "https://github.com/kaishin/Thyme",
    "social_media_url": "http://twitter.com/kaishin",
    "license": {
        "type": "BSD",
        "file": "LICENSE"
    },
    "authors": {
        "Reda Lemeden": "[email protected]"
    },
    "source": {
        "git": "https://github.com/kaishin/Thyme.git",
        "tag": "v2.0"
    },
    "ios": {
        "source_files": [
            "Thyme/**/*.{h,swift}",
            "Shared/**/*.{h,swift}"
        ]
    },
    "osx": {
        "source_files": [
            "ThymeMac/**/*.{h,swift}",
            "Shared/**/*.{h,swift}"
        ]
    },
    "requires_arc": true,
    "platforms": {
        "ios": "10.0",
        "osx": "10.10"
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This