-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStylization.swift
82 lines (63 loc) · 2.54 KB
/
Stylization.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Stylization.swift
// project-mars
//
// Created by Aleksandr Grin on 1/3/17.
// Copyright © 2017 AleksandrGrin. All rights reserved.
//
import Foundation
import UIKit
extension UIView{
func setMainBackgroundwithFit(backgroundName: String){
let bgImage = UIImage(named: backgroundName)!
let height = UIScreen.main.bounds.height
let width = UIScreen.main.bounds.width
let imageFrame = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageFrame.image = bgImage
self.addSubview(imageFrame)
self.sendSubview(toBack: imageFrame)
}
func setSecondaryBackground(named: String){
let bgImage = UIImage(named: named)!
let imageRect = self.bounds
let imageFrame = UIImageView(frame: imageRect)
imageFrame.image = bgImage
self.addSubview(imageFrame)
self.sendSubview(toBack: imageFrame)
}
}
extension UILabel{
func setLabelBackground(backgroundName: String){
let bgImage = UIImage(named: backgroundName)!
let height = self.bounds.height
let width = self.bounds.width
UIGraphicsBeginImageContext(self.bounds.size)
bgImage.draw(in: CGRect(x: 0, y: 0, width: width, height: height),blendMode: CGBlendMode.colorBurn, alpha: 0.9)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.backgroundColor = UIColor(patternImage: image!)
}
}
extension UIBezierPath {
class func arrow(from start: CGPoint, to end: CGPoint, tailWidth: CGFloat, headWidth: CGFloat, headLength: CGFloat) -> Self {
let length = hypot(end.x - start.x, end.y - start.y)
let tailLength = length - headLength
func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint { return CGPoint(x: x, y: y) }
let points: [CGPoint] = [
p(0, tailWidth / 2),
p(tailLength, tailWidth / 2),
p(tailLength, headWidth / 2),
p(length, 0),
p(tailLength, -headWidth / 2),
p(tailLength, -tailWidth / 2),
p(0, -tailWidth / 2)
]
let cosine = (end.x - start.x) / length
let sine = (end.y - start.y) / length
let transform = CGAffineTransform(a: cosine, b: sine, c: -sine, d: cosine, tx: start.x, ty: start.y)
let path = CGMutablePath()
path.addLines(between: points, transform: transform)
path.closeSubpath()
return self.init(cgPath: path)
}
}