Created
August 28, 2019 09:12
-
-
Save highthem/2f40f0b73c7acd35563429e1cd553944 to your computer and use it in GitHub Desktop.
how to use exported style from Paint Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Assets.swift | |
// Hello | |
// | |
// Created by Haithem on 28/08/2019. | |
// Copyright © 2019 highthem. All rights reserved. | |
// | |
// Generated by PaintCode | |
// http://www.paintcodeapp.com | |
// | |
import UIKit | |
public class Assets : NSObject { | |
//// Cache | |
private struct Cache { | |
static let gold: UIColor = UIColor(red: 0.937, green: 0.808, blue: 0.290, alpha: 1.000) | |
static let green: UIColor = UIColor(red: 0.145, green: 0.682, blue: 0.533, alpha: 1.000) | |
static let red: UIColor = UIColor(red: 0.843, green: 0.353, blue: 0.290, alpha: 1.000) | |
static var imageOfFailfilled: UIImage? | |
static var failfilledTargets: [AnyObject]? | |
} | |
//// Colors | |
@objc dynamic public class var gold: UIColor { return Cache.gold } | |
@objc dynamic public class var green: UIColor { return Cache.green } | |
@objc dynamic public class var red: UIColor { return Cache.red } | |
//// Drawing Methods | |
@objc dynamic public class func drawStarfilled(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 50, height: 50), resizing: ResizingBehavior = .aspectFit) { | |
//// General Declarations | |
let context = UIGraphicsGetCurrentContext()! | |
//// Resize to Target Frame | |
context.saveGState() | |
let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 50, height: 50), target: targetFrame) | |
context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) | |
context.scaleBy(x: resizedFrame.width / 50, y: resizedFrame.height / 50) | |
//// Star Drawing | |
context.saveGState() | |
context.translateBy(x: 25.42, y: 27) | |
let starPath = UIBezierPath() | |
starPath.move(to: CGPoint(x: -0.57, y: -25)) | |
starPath.addLine(to: CGPoint(x: 6.77, y: -10.11)) | |
starPath.addLine(to: CGPoint(x: 23.2, y: -7.73)) | |
starPath.addLine(to: CGPoint(x: 11.31, y: 3.86)) | |
starPath.addLine(to: CGPoint(x: 14.12, y: 20.23)) | |
starPath.addLine(to: CGPoint(x: -0.57, y: 12.5)) | |
starPath.addLine(to: CGPoint(x: -15.27, y: 20.23)) | |
starPath.addLine(to: CGPoint(x: -12.46, y: 3.86)) | |
starPath.addLine(to: CGPoint(x: -24.35, y: -7.73)) | |
starPath.addLine(to: CGPoint(x: -7.92, y: -10.11)) | |
starPath.close() | |
Assets.gold.setFill() | |
starPath.fill() | |
context.restoreGState() | |
context.restoreGState() | |
} | |
@objc dynamic public class func drawCheckfilled(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 50, height: 50), resizing: ResizingBehavior = .aspectFit) { | |
//// General Declarations | |
let context = UIGraphicsGetCurrentContext()! | |
//// Resize to Target Frame | |
context.saveGState() | |
let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 50, height: 50), target: targetFrame) | |
context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) | |
context.scaleBy(x: resizedFrame.width / 50, y: resizedFrame.height / 50) | |
//// Color Declarations | |
let white = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) | |
//// Oval Drawing | |
let ovalPath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 50, height: 50)) | |
Assets.green.setFill() | |
ovalPath.fill() | |
//// Bezier Drawing | |
let bezierPath = UIBezierPath() | |
bezierPath.move(to: CGPoint(x: 38, y: 15)) | |
bezierPath.addLine(to: CGPoint(x: 22, y: 33)) | |
bezierPath.addLine(to: CGPoint(x: 12, y: 25)) | |
white.setStroke() | |
bezierPath.lineWidth = 2 | |
bezierPath.lineCapStyle = .round | |
bezierPath.lineJoinStyle = .round | |
bezierPath.stroke() | |
context.restoreGState() | |
} | |
@objc dynamic public class func drawFailfilled(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 50, height: 50), resizing: ResizingBehavior = .aspectFit) { | |
//// General Declarations | |
let context = UIGraphicsGetCurrentContext()! | |
//// Resize to Target Frame | |
context.saveGState() | |
let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 50, height: 50), target: targetFrame) | |
context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) | |
context.scaleBy(x: resizedFrame.width / 50, y: resizedFrame.height / 50) | |
//// Color Declarations | |
let white = UIColor(red: 1.000, green: 1.000, blue: 1.000, alpha: 1.000) | |
//// Oval Drawing | |
let ovalPath = UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 50, height: 50)) | |
Assets.red.setFill() | |
ovalPath.fill() | |
//// Bezier Drawing | |
let bezierPath = UIBezierPath() | |
bezierPath.move(to: CGPoint(x: 16, y: 34)) | |
bezierPath.addLine(to: CGPoint(x: 25, y: 25)) | |
bezierPath.addLine(to: CGPoint(x: 34, y: 16)) | |
white.setStroke() | |
bezierPath.lineWidth = 2 | |
bezierPath.lineCapStyle = .round | |
bezierPath.stroke() | |
//// Bezier 2 Drawing | |
let bezier2Path = UIBezierPath() | |
bezier2Path.move(to: CGPoint(x: 16, y: 16)) | |
bezier2Path.addLine(to: CGPoint(x: 25, y: 25)) | |
bezier2Path.addLine(to: CGPoint(x: 34, y: 34)) | |
white.setStroke() | |
bezier2Path.lineWidth = 2 | |
bezier2Path.lineCapStyle = .round | |
bezier2Path.stroke() | |
context.restoreGState() | |
} | |
//// Generated Images | |
@objc dynamic public class var imageOfFailfilled: UIImage { | |
if Cache.imageOfFailfilled != nil { | |
return Cache.imageOfFailfilled! | |
} | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: 50, height: 50), false, 0) | |
Assets.drawFailfilled() | |
Cache.imageOfFailfilled = UIGraphicsGetImageFromCurrentImageContext()! | |
UIGraphicsEndImageContext() | |
return Cache.imageOfFailfilled! | |
} | |
//// Customization Infrastructure | |
@objc @IBOutlet dynamic var failfilledTargets: [AnyObject]! { | |
get { return Cache.failfilledTargets } | |
set { | |
Cache.failfilledTargets = newValue | |
for target: AnyObject in newValue { | |
let _ = target.perform(NSSelectorFromString("setImage:"), with: Assets.imageOfFailfilled) | |
} | |
} | |
} | |
@objc(AssetsResizingBehavior) | |
public enum ResizingBehavior: Int { | |
case aspectFit /// The content is proportionally resized to fit into the target rectangle. | |
case aspectFill /// The content is proportionally resized to completely fill the target rectangle. | |
case stretch /// The content is stretched to match the entire target rectangle. | |
case center /// The content is centered in the target rectangle, but it is NOT resized. | |
public func apply(rect: CGRect, target: CGRect) -> CGRect { | |
if rect == target || target == CGRect.zero { | |
return rect | |
} | |
var scales = CGSize.zero | |
scales.width = abs(target.width / rect.width) | |
scales.height = abs(target.height / rect.height) | |
switch self { | |
case .aspectFit: | |
scales.width = min(scales.width, scales.height) | |
scales.height = scales.width | |
case .aspectFill: | |
scales.width = max(scales.width, scales.height) | |
scales.height = scales.width | |
case .stretch: | |
break | |
case .center: | |
scales.width = 1 | |
scales.height = 1 | |
} | |
var result = rect.standardized | |
result.size.width *= scales.width | |
result.size.height *= scales.height | |
result.origin.x = target.minX + (target.width - result.width) / 2 | |
result.origin.y = target.minY + (target.height - result.height) / 2 | |
return result | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import PlaygroundSupport | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
let label = UILabel() | |
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20) | |
label.text = "Hello World!" | |
label.textColor = .black | |
view.addSubview(label) | |
let star = UIImageView(image: paintImage(.star)) | |
let check = UIImageView(image: paintImage(.check)) | |
let fail = UIImageView(image: paintImage(.fail)) | |
let stack = UIStackView(frame: CGRect(x: 10, y: 300, width: 150, height: 50)) | |
stack.addArrangedSubview(star) | |
stack.addArrangedSubview(check) | |
stack.addArrangedSubview(fail) | |
view.addSubview(stack) | |
self.view = view | |
} | |
private enum Images { | |
case star, check, fail | |
func draw() { | |
switch self { | |
case .star: Assets.drawStarfilled() | |
case .check: Assets.drawCheckfilled() | |
case .fail: Assets.drawFailfilled() | |
} | |
} | |
} | |
private func paintImage(_ img: Images) -> UIImage { | |
UIGraphicsBeginImageContext(CGSize(width: 50, height: 50)) | |
img.draw() | |
let img = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return img ?? UIImage() | |
} | |
} | |
PlaygroundPage.current.liveView = MyViewController() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment