|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2019 Ivan Vorobei ( [email protected]) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +import UIKit |
| 23 | + |
| 24 | +public class SPAlertIconCardView: UIView { |
| 25 | + |
| 26 | + init() { |
| 27 | + super.init(frame: .zero) |
| 28 | + self.backgroundColor = .clear |
| 29 | + } |
| 30 | + |
| 31 | + required init?(coder aDecoder: NSCoder) { |
| 32 | + fatalError("init(coder:) has not been implemented") |
| 33 | + } |
| 34 | + |
| 35 | + public override func draw(_ rect: CGRect) { |
| 36 | + super.draw(rect) |
| 37 | + CardDraw.draw(frame: rect, resizing: .aspectFit, fillColor: self.tintColor) |
| 38 | + } |
| 39 | + |
| 40 | + class CardDraw: NSObject { |
| 41 | + |
| 42 | + @objc dynamic public class func draw(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 60, height: 56), resizing: ResizingBehavior = .aspectFit, fillColor: UIColor = UIColor(red: 0.349, green: 0.345, blue: 0.353, alpha: 1.000)) { |
| 43 | + let context = UIGraphicsGetCurrentContext()! |
| 44 | + context.saveGState() |
| 45 | + let resizedFrame: CGRect = resizing.apply(rect: CGRect(x: 0, y: 0, width: 67, height: 59), target: targetFrame) |
| 46 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 47 | + context.scaleBy(x: resizedFrame.width / 67, y: resizedFrame.height / 59) |
| 48 | + let bezierPath = UIBezierPath() |
| 49 | + bezierPath.move(to: CGPoint(x: 3, y: 18.02)) |
| 50 | + bezierPath.addLine(to: CGPoint(x: 63.06, y: 18.02)) |
| 51 | + bezierPath.addLine(to: CGPoint(x: 63.06, y: 15.48)) |
| 52 | + bezierPath.addCurve(to: CGPoint(x: 54.47, y: 7), controlPoint1: CGPoint(x: 63.06, y: 9.84), controlPoint2: CGPoint(x: 60.19, y: 7)) |
| 53 | + bezierPath.addLine(to: CGPoint(x: 11.59, y: 7)) |
| 54 | + bezierPath.addCurve(to: CGPoint(x: 3, y: 15.48), controlPoint1: CGPoint(x: 5.84, y: 7), controlPoint2: CGPoint(x: 3, y: 9.84)) |
| 55 | + bezierPath.addLine(to: CGPoint(x: 3, y: 18.02)) |
| 56 | + bezierPath.close() |
| 57 | + bezierPath.move(to: CGPoint(x: 3, y: 44.55)) |
| 58 | + bezierPath.addCurve(to: CGPoint(x: 11.59, y: 53), controlPoint1: CGPoint(x: 3, y: 50.18), controlPoint2: CGPoint(x: 5.84, y: 53)) |
| 59 | + bezierPath.addLine(to: CGPoint(x: 54.47, y: 53)) |
| 60 | + bezierPath.addCurve(to: CGPoint(x: 63.06, y: 44.55), controlPoint1: CGPoint(x: 60.19, y: 53), controlPoint2: CGPoint(x: 63.06, y: 50.18)) |
| 61 | + bezierPath.addLine(to: CGPoint(x: 63.06, y: 24.23)) |
| 62 | + bezierPath.addLine(to: CGPoint(x: 3, y: 24.23)) |
| 63 | + bezierPath.addLine(to: CGPoint(x: 3, y: 44.55)) |
| 64 | + bezierPath.close() |
| 65 | + bezierPath.move(to: CGPoint(x: 12.13, y: 37.88)) |
| 66 | + bezierPath.addLine(to: CGPoint(x: 12.13, y: 32.82)) |
| 67 | + bezierPath.addCurve(to: CGPoint(x: 14.81, y: 30.19), controlPoint1: CGPoint(x: 12.13, y: 31.29), controlPoint2: CGPoint(x: 13.2, y: 30.19)) |
| 68 | + bezierPath.addLine(to: CGPoint(x: 21.51, y: 30.19)) |
| 69 | + bezierPath.addCurve(to: CGPoint(x: 24.2, y: 32.82), controlPoint1: CGPoint(x: 23.13, y: 30.19), controlPoint2: CGPoint(x: 24.2, y: 31.29)) |
| 70 | + bezierPath.addLine(to: CGPoint(x: 24.2, y: 37.88)) |
| 71 | + bezierPath.addCurve(to: CGPoint(x: 21.51, y: 40.5), controlPoint1: CGPoint(x: 24.2, y: 39.44), controlPoint2: CGPoint(x: 23.13, y: 40.5)) |
| 72 | + bezierPath.addLine(to: CGPoint(x: 14.81, y: 40.5)) |
| 73 | + bezierPath.addCurve(to: CGPoint(x: 12.13, y: 37.88), controlPoint1: CGPoint(x: 13.2, y: 40.5), controlPoint2: CGPoint(x: 12.13, y: 39.44)) |
| 74 | + bezierPath.close() |
| 75 | + fillColor.setFill() |
| 76 | + bezierPath.fill() |
| 77 | + context.restoreGState() |
| 78 | + } |
| 79 | + |
| 80 | + @objc(StyleKitNameResizingBehavior) |
| 81 | + public enum ResizingBehavior: Int { |
| 82 | + |
| 83 | + case aspectFit |
| 84 | + case aspectFill |
| 85 | + case stretch |
| 86 | + case center |
| 87 | + |
| 88 | + public func apply(rect: CGRect, target: CGRect) -> CGRect { |
| 89 | + if rect == target || target == CGRect.zero { |
| 90 | + return rect |
| 91 | + } |
| 92 | + |
| 93 | + var scales = CGSize.zero |
| 94 | + scales.width = abs(target.width / rect.width) |
| 95 | + scales.height = abs(target.height / rect.height) |
| 96 | + |
| 97 | + switch self { |
| 98 | + case .aspectFit: |
| 99 | + scales.width = min(scales.width, scales.height) |
| 100 | + scales.height = scales.width |
| 101 | + case .aspectFill: |
| 102 | + scales.width = max(scales.width, scales.height) |
| 103 | + scales.height = scales.width |
| 104 | + case .stretch: |
| 105 | + break |
| 106 | + case .center: |
| 107 | + scales.width = 1 |
| 108 | + scales.height = 1 |
| 109 | + } |
| 110 | + |
| 111 | + var result = rect.standardized |
| 112 | + result.size.width *= scales.width |
| 113 | + result.size.height *= scales.height |
| 114 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 115 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 116 | + return result |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments