Created
November 11, 2016 14:15
-
-
Save detaybey/214b23731a3b4d0344ce58643795f4b7 to your computer and use it in GitHub Desktop.
SKLabelNode with outerline for Swift 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LFShadowLabel.swift | |
// | |
// Created by Umut Çelenli on 10/11/2016. | |
// Copyright © 2016 PanelBir. All rights reserved. | |
// | |
import Foundation | |
import SpriteKit | |
class LFOutlinedLabel : SKSpriteNode { | |
private let skewX : [CGFloat] = [-1, 1, 1,-1] | |
private let skewY : [CGFloat] = [-1,-1, 1, 1] | |
private var label : SKLabelNode = SKLabelNode() | |
private var shadows : [SKLabelNode] = [] | |
public var borderOpacity : CGFloat = 1 | |
public var borderSize: CGFloat = 1 | |
public var borderColor: UIColor = UIColor.black | |
public var text : String = "?" | |
public var fontName : String = Fonts.OptimaExtraBlack.rawValue | |
public var fontColor: UIColor = UIColor.white | |
public var fontSize : CGFloat = 40 | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
//self.setup() | |
} | |
override init(texture: SKTexture!, color: UIColor, size: CGSize) { | |
super.init(texture: texture, color: color, size: size) | |
//self.setup() | |
} | |
convenience init(size: CGSize, font: String, fSize: CGFloat, fColor: UIColor, bSize: CGFloat, bColor: UIColor, bOpacity: CGFloat) | |
{ | |
self.init(texture: nil, color: UIColor.clear, size: size) | |
self.fontName = font | |
self.fontSize = fSize | |
self.fontColor = fColor | |
self.borderSize = bSize | |
self.borderColor = bColor | |
self.borderOpacity = bOpacity | |
self.setup() | |
} | |
// create shadow labels | |
private func setup() { | |
if shadows.count == 0 { | |
let width = self.size.width / 2 | |
let height = self.size.height / 2 | |
for j in 0...3 { | |
let shadow = SKLabelNode(text: self.text) | |
addChild(shadow) | |
shadow.verticalAlignmentMode = .center | |
shadow.horizontalAlignmentMode = .center | |
shadow.zPosition = 999 | |
shadow.position = CGPoint(x: width + (skewX[j] * borderSize) , y: height + (skewY[j] * borderSize)) | |
shadow.text = self.text | |
shadow.fontSize = self.fontSize | |
shadow.fontName = self.fontName | |
shadow.fontColor = borderColor | |
shadows.append(shadow) | |
} | |
let label = SKLabelNode(text: self.text) | |
addChild(label) | |
label.verticalAlignmentMode = .center | |
label.horizontalAlignmentMode = .center | |
label.zPosition = 1000 | |
label.position = CGPoint(x: width , y: height ) | |
label.text = self.text | |
label.fontSize = self.fontSize | |
label.fontName = self.fontName | |
label.fontColor = fontColor | |
self.label = label | |
} | |
} | |
public func update(){ | |
let width = self.size.width / 2 | |
let height = self.size.height / 2 | |
self.label.fontSize = fontSize | |
self.label.fontName = fontName | |
self.label.fontColor = fontColor | |
self.label.verticalAlignmentMode = .center | |
self.label.horizontalAlignmentMode = .center | |
self.label.text = text | |
self.label.position = CGPoint(x: width , y: height ) | |
for i in 0...3 { | |
shadows[i].verticalAlignmentMode = .center | |
shadows[i].horizontalAlignmentMode = .center | |
shadows[i].fontColor = borderColor | |
shadows[i].fontSize = fontSize | |
shadows[i].alpha = borderOpacity | |
shadows[i].fontName = fontName | |
shadows[i].text = text | |
shadows[i].position = CGPoint(x: width + (skewX[i] * borderSize) , y: height + (skewY[i] * borderSize) ) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment