|
| 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 SPAlertIconQuestionView: 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 | + QuestionDraw.draw(frame: rect, resizing: .aspectFit, fillColor: self.tintColor) |
| 38 | + } |
| 39 | + |
| 40 | + class QuestionDraw: 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: 76, height: 74), target: targetFrame) |
| 46 | + context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY) |
| 47 | + context.scaleBy(x: resizedFrame.width / 76, y: resizedFrame.height / 74) |
| 48 | + let bezierPath = UIBezierPath() |
| 49 | + bezierPath.move(to: CGPoint(x: 30.56, y: 68.4)) |
| 50 | + bezierPath.addCurve(to: CGPoint(x: 43.85, y: 68.34), controlPoint1: CGPoint(x: 34.48, y: 72.31), controlPoint2: CGPoint(x: 39.94, y: 72.25)) |
| 51 | + bezierPath.addLine(to: CGPoint(x: 68.34, y: 43.85)) |
| 52 | + bezierPath.addCurve(to: CGPoint(x: 68.4, y: 30.56), controlPoint1: CGPoint(x: 72.25, y: 39.94), controlPoint2: CGPoint(x: 72.31, y: 34.45)) |
| 53 | + bezierPath.addLine(to: CGPoint(x: 43.73, y: 5.86)) |
| 54 | + bezierPath.addCurve(to: CGPoint(x: 30.44, y: 5.99), controlPoint1: CGPoint(x: 39.81, y: 1.98), controlPoint2: CGPoint(x: 34.36, y: 2.07)) |
| 55 | + bezierPath.addLine(to: CGPoint(x: 5.99, y: 30.44)) |
| 56 | + bezierPath.addCurve(to: CGPoint(x: 5.86, y: 43.73), controlPoint1: CGPoint(x: 2.07, y: 34.36), controlPoint2: CGPoint(x: 1.98, y: 39.81)) |
| 57 | + bezierPath.addLine(to: CGPoint(x: 30.56, y: 68.4)) |
| 58 | + bezierPath.close() |
| 59 | + bezierPath.move(to: CGPoint(x: 36.21, y: 45.43)) |
| 60 | + bezierPath.addCurve(to: CGPoint(x: 33.62, y: 42.9), controlPoint1: CGPoint(x: 34.54, y: 45.43), controlPoint2: CGPoint(x: 33.62, y: 44.56)) |
| 61 | + bezierPath.addLine(to: CGPoint(x: 33.62, y: 42.47)) |
| 62 | + bezierPath.addCurve(to: CGPoint(x: 37.66, y: 35.96), controlPoint1: CGPoint(x: 33.62, y: 39.29), controlPoint2: CGPoint(x: 35.34, y: 37.56)) |
| 63 | + bezierPath.addLine(to: CGPoint(x: 38.47, y: 35.4)) |
| 64 | + bezierPath.addCurve(to: CGPoint(x: 41.88, y: 30.69), controlPoint1: CGPoint(x: 40.78, y: 33.78), controlPoint2: CGPoint(x: 41.88, y: 32.69)) |
| 65 | + bezierPath.addCurve(to: CGPoint(x: 37.01, y: 26.59), controlPoint1: CGPoint(x: 41.88, y: 28.25), controlPoint2: CGPoint(x: 39.97, y: 26.59)) |
| 66 | + bezierPath.addCurve(to: CGPoint(x: 32.23, y: 29.42), controlPoint1: CGPoint(x: 34.85, y: 26.59), controlPoint2: CGPoint(x: 33.22, y: 27.67)) |
| 67 | + bezierPath.addCurve(to: CGPoint(x: 29.05, y: 31.61), controlPoint1: CGPoint(x: 31.18, y: 30.66), controlPoint2: CGPoint(x: 30.9, y: 31.61)) |
| 68 | + bezierPath.addCurve(to: CGPoint(x: 26.83, y: 29.39), controlPoint1: CGPoint(x: 27.97, y: 31.61), controlPoint2: CGPoint(x: 26.83, y: 30.84)) |
| 69 | + bezierPath.addCurve(to: CGPoint(x: 27.08, y: 27.79), controlPoint1: CGPoint(x: 26.83, y: 28.84), controlPoint2: CGPoint(x: 26.92, y: 28.31)) |
| 70 | + bezierPath.addCurve(to: CGPoint(x: 37.19, y: 22.18), controlPoint1: CGPoint(x: 27.91, y: 24.8), controlPoint2: CGPoint(x: 31.64, y: 22.18)) |
| 71 | + bezierPath.addCurve(to: CGPoint(x: 47.49, y: 30.44), controlPoint1: CGPoint(x: 42.71, y: 22.18), controlPoint2: CGPoint(x: 47.49, y: 25.07)) |
| 72 | + bezierPath.addCurve(to: CGPoint(x: 42.13, y: 38.3), controlPoint1: CGPoint(x: 47.49, y: 34.33), controlPoint2: CGPoint(x: 45.24, y: 36.18)) |
| 73 | + bezierPath.addCurve(to: CGPoint(x: 38.8, y: 42.74), controlPoint1: CGPoint(x: 39.88, y: 39.81), controlPoint2: CGPoint(x: 38.8, y: 40.92)) |
| 74 | + bezierPath.addLine(to: CGPoint(x: 38.8, y: 43.14)) |
| 75 | + bezierPath.addCurve(to: CGPoint(x: 36.21, y: 45.43), controlPoint1: CGPoint(x: 38.8, y: 44.41), controlPoint2: CGPoint(x: 37.84, y: 45.43)) |
| 76 | + bezierPath.close() |
| 77 | + bezierPath.move(to: CGPoint(x: 36.33, y: 54.71)) |
| 78 | + bezierPath.addCurve(to: CGPoint(x: 32.85, y: 51.38), controlPoint1: CGPoint(x: 34.54, y: 54.71), controlPoint2: CGPoint(x: 32.85, y: 53.29)) |
| 79 | + bezierPath.addCurve(to: CGPoint(x: 36.33, y: 48.05), controlPoint1: CGPoint(x: 32.85, y: 49.47), controlPoint2: CGPoint(x: 34.51, y: 48.05)) |
| 80 | + bezierPath.addCurve(to: CGPoint(x: 39.78, y: 51.38), controlPoint1: CGPoint(x: 38.12, y: 48.05), controlPoint2: CGPoint(x: 39.78, y: 49.43)) |
| 81 | + bezierPath.addCurve(to: CGPoint(x: 36.33, y: 54.71), controlPoint1: CGPoint(x: 39.78, y: 53.32), controlPoint2: CGPoint(x: 38.09, y: 54.71)) |
| 82 | + bezierPath.close() |
| 83 | + fillColor.setFill() |
| 84 | + bezierPath.fill() |
| 85 | + context.restoreGState() |
| 86 | + } |
| 87 | + |
| 88 | + @objc(StyleKitNameResizingBehavior) |
| 89 | + public enum ResizingBehavior: Int { |
| 90 | + |
| 91 | + case aspectFit |
| 92 | + case aspectFill |
| 93 | + case stretch |
| 94 | + case center |
| 95 | + |
| 96 | + public func apply(rect: CGRect, target: CGRect) -> CGRect { |
| 97 | + if rect == target || target == CGRect.zero { |
| 98 | + return rect |
| 99 | + } |
| 100 | + |
| 101 | + var scales = CGSize.zero |
| 102 | + scales.width = abs(target.width / rect.width) |
| 103 | + scales.height = abs(target.height / rect.height) |
| 104 | + |
| 105 | + switch self { |
| 106 | + case .aspectFit: |
| 107 | + scales.width = min(scales.width, scales.height) |
| 108 | + scales.height = scales.width |
| 109 | + case .aspectFill: |
| 110 | + scales.width = max(scales.width, scales.height) |
| 111 | + scales.height = scales.width |
| 112 | + case .stretch: |
| 113 | + break |
| 114 | + case .center: |
| 115 | + scales.width = 1 |
| 116 | + scales.height = 1 |
| 117 | + } |
| 118 | + |
| 119 | + var result = rect.standardized |
| 120 | + result.size.width *= scales.width |
| 121 | + result.size.height *= scales.height |
| 122 | + result.origin.x = target.minX + (target.width - result.width) / 2 |
| 123 | + result.origin.y = target.minY + (target.height - result.height) / 2 |
| 124 | + return result |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments