-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.swift
More file actions
118 lines (104 loc) · 3.38 KB
/
Extensions.swift
File metadata and controls
118 lines (104 loc) · 3.38 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// 2DArrayAccess.swift
// HexWars
//
// Created by Aleksandr Grin on 8/5/17.
// Copyright © 2017 AleksandrGrin. All rights reserved.
//
import Foundation
import UIKit
import SpriteKit
//Allows you to access a column of a 2D array
extension Array where Element : Collection {
subscript(column column: Element.Index) -> [ Element.Iterator.Element ] {
return map{ $0[ column ] }
}
}
// Easily change the pieces of a frame of a view manually
extension UIView {
func setOrigin(x:CGFloat) {
var frame:CGRect = self.frame
frame.origin.x = x
self.frame = frame
}
func setOrigin(y:CGFloat) {
var frame:CGRect = self.frame
frame.origin.y = y
self.frame = frame
}
func setWidth(width:CGFloat) {
var frame:CGRect = self.frame
frame.size.width = width
self.frame = frame
}
func setHeight(height:CGFloat) {
var frame:CGRect = self.frame
frame.size.height = height
self.frame = frame
}
}
// Converts a view into an image essentially taking a "screenshot"
extension UIView {
func asImage() -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
return renderer.image{ rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
// Since all of our VCs are embedded in a navigation controller, we must set the orientation control here
extension UINavigationController {
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
}
extension Array {
func randomItem() -> Element? {
if isEmpty { return nil }
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
}
extension SKLabelNode {
func multilined() -> SKLabelNode {
let substrings: [String] = self.text!.components(separatedBy: "\n")
return substrings.enumerated().reduce(SKLabelNode()) {
let label = SKLabelNode(fontNamed: self.fontName)
label.text = $1.element
label.fontColor = self.fontColor
label.fontSize = self.fontSize
label.position = self.position
label.zPosition = self.zPosition
label.horizontalAlignmentMode = self.horizontalAlignmentMode
label.verticalAlignmentMode = self.verticalAlignmentMode
let y = CGFloat($1.offset - substrings.count / 2) * self.fontSize
label.position = CGPoint(x: 0, y: -y)
$0.addChild(label)
return $0
}
}
}
extension UIColor {
var name: String? {
switch self {
case UIColor.black: return "black"
case UIColor.darkGray: return "darkGray"
case UIColor.lightGray: return "lightGray"
case UIColor.white: return "white"
case UIColor.gray: return "gray"
case UIColor.red: return "red"
case UIColor.green: return "green"
case UIColor.blue: return "blue"
case UIColor.cyan: return "cyan"
case UIColor.yellow: return "yellow"
case UIColor.magenta: return "magenta"
case UIColor.orange: return "orange"
case UIColor.purple: return "purple"
case UIColor.brown: return "brown"
default: return nil
}
}
}