|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2019 Ivan Vorobei (ivanvorobei@icloud.com) |
| 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 SPAlertIconBookmarkView: 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 | + BookmarkDraw.draw(frame: rect, resizing: .aspectFit, fillColor: self.tintColor) |
| 38 | + } |
| 39 | + |
| 40 | + class BookmarkDraw: NSObject { |
| 41 | + |
| 42 | + @objc dynamic public class func draw(frame targetFrame: CGRect = CGRect(x: 0, y: 0, width: 40, height: 53), 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: 40, height: 53), target: targetFrame) |
| 46 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 47 | + context.scaleBy(x: resizedFrame.width / 40, y: resizedFrame.height / 53) |
| 48 | + let bezierPath = UIBezierPath() |
| 49 | + bezierPath.move(to: CGPoint(x: 4.15, y: 52)) |
| 50 | + bezierPath.addCurve(to: CGPoint(x: 1, y: 48.46), controlPoint1: CGPoint(x: 2.4, y: 52), controlPoint2: CGPoint(x: 1, y: 50.93)) |
| 51 | + bezierPath.addLine(to: CGPoint(x: 1, y: 8.67)) |
| 52 | + bezierPath.addCurve(to: CGPoint(x: 8.6, y: 1), controlPoint1: CGPoint(x: 1, y: 3.63), controlPoint2: CGPoint(x: 3.61, y: 1)) |
| 53 | + bezierPath.addLine(to: CGPoint(x: 31.29, y: 1)) |
| 54 | + bezierPath.addCurve(to: CGPoint(x: 38.88, y: 8.67), controlPoint1: CGPoint(x: 36.27, y: 1), controlPoint2: CGPoint(x: 38.88, y: 3.63)) |
| 55 | + bezierPath.addLine(to: CGPoint(x: 38.88, y: 48.46)) |
| 56 | + bezierPath.addCurve(to: CGPoint(x: 35.71, y: 52), controlPoint1: CGPoint(x: 38.88, y: 50.93), controlPoint2: CGPoint(x: 37.46, y: 52)) |
| 57 | + bezierPath.addCurve(to: CGPoint(x: 32.52, y: 50.32), controlPoint1: CGPoint(x: 34.39, y: 52), controlPoint2: CGPoint(x: 33.55, y: 51.3)) |
| 58 | + bezierPath.addLine(to: CGPoint(x: 21.04, y: 39.35)) |
| 59 | + bezierPath.addCurve(to: CGPoint(x: 19.94, y: 38.74), controlPoint1: CGPoint(x: 20.55, y: 38.91), controlPoint2: CGPoint(x: 20.24, y: 38.74)) |
| 60 | + bezierPath.addCurve(to: CGPoint(x: 18.85, y: 39.35), controlPoint1: CGPoint(x: 19.64, y: 38.74), controlPoint2: CGPoint(x: 19.34, y: 38.88)) |
| 61 | + bezierPath.addLine(to: CGPoint(x: 8.5, y: 49.18)) |
| 62 | + bezierPath.addLine(to: CGPoint(x: 7.65, y: 49.97)) |
| 63 | + bezierPath.addCurve(to: CGPoint(x: 4.15, y: 52), controlPoint1: CGPoint(x: 6.04, y: 51.46), controlPoint2: CGPoint(x: 5.31, y: 52)) |
| 64 | + bezierPath.close() |
| 65 | + fillColor.setFill() |
| 66 | + bezierPath.fill() |
| 67 | + context.restoreGState() |
| 68 | + } |
| 69 | + |
| 70 | + @objc(StyleKitNameResizingBehavior) |
| 71 | + public enum ResizingBehavior: Int { |
| 72 | + |
| 73 | + case aspectFit |
| 74 | + case aspectFill |
| 75 | + case stretch |
| 76 | + case center |
| 77 | + |
| 78 | + public func apply(rect: CGRect, target: CGRect) -> CGRect { |
| 79 | + if rect == target || target == CGRect.zero { |
| 80 | + return rect |
| 81 | + } |
| 82 | + |
| 83 | + var scales = CGSize.zero |
| 84 | + scales.width = abs(target.width / rect.width) |
| 85 | + scales.height = abs(target.height / rect.height) |
| 86 | + |
| 87 | + switch self { |
| 88 | + case .aspectFit: |
| 89 | + scales.width = min(scales.width, scales.height) |
| 90 | + scales.height = scales.width |
| 91 | + case .aspectFill: |
| 92 | + scales.width = max(scales.width, scales.height) |
| 93 | + scales.height = scales.width |
| 94 | + case .stretch: |
| 95 | + break |
| 96 | + case .center: |
| 97 | + scales.width = 1 |
| 98 | + scales.height = 1 |
| 99 | + } |
| 100 | + |
| 101 | + var result = rect.standardized |
| 102 | + result.size.width *= scales.width |
| 103 | + result.size.height *= scales.height |
| 104 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 105 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 106 | + return result |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments