-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMessageNode.swift
54 lines (39 loc) · 1.51 KB
/
MessageNode.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
//
// MessageNode.swift
// Pelmanism
//
// Created by Julian Moorhouse on 20/09/2019.
// Copyright © 2019 Mindwarp Consultancy Ltd. All rights reserved.
//
import SpriteKit
import UIKit
class MessageNode: SKSpriteNode {
func setup(message: String, fontSize: CGFloat) {
name = "message"
let image: UIImage = drawCardFront(size: size, message: message, fontSize: fontSize)
texture = SKTexture(image: image)
configurePhysics()
}
func configurePhysics() {
physicsBody = SKPhysicsBody(texture: texture!, size: size)
physicsBody?.isDynamic = false
}
func drawCardFront(size: CGSize, message: String, fontSize: CGFloat) -> UIImage {
let renderer = UIGraphicsImageRenderer(size: size)
let img = renderer.image { ctx in
let rect = CGRect(origin: CGPoint(x: 10, y: 20), size: size)
let shadow = NSShadow()
shadow.shadowColor = UIColor.white
shadow.shadowBlurRadius = 10
let textFontAttributes: [NSAttributedString.Key : Any] = [
.font: UIFont.systemFont(ofSize: fontSize),
.foregroundColor: color,
.shadow: shadow
]
let string = NSAttributedString(string: message, attributes: textFontAttributes)
string.draw(in: rect)
ctx.cgContext.drawPath(using: .fill)
}
return img
}
}