텍스트를 CGContext
에 그 다음 텍스처를 만들고 SKSpriteNode
에 해당 텍스처를 지정할 수 있습니다. 여기
이
this GitHub project에서 예입니다 : 그 짓을하고 파악 된 후에는
class ASAttributedLabelNode: SKSpriteNode {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(size: CGSize) {
super.init(texture: nil, color: UIColor.clear, size: size)
}
var attributedString: NSAttributedString! {
didSet {
draw()
}
}
func draw() {
guard let attrStr = attributedString else {
texture = nil
return
}
let scaleFactor = UIScreen.main.scale
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
guard let context = CGContext(data: nil, width: Int(size.width * scaleFactor), height: Int(size.height * scaleFactor), bitsPerComponent: 8, bytesPerRow: Int(size.width * scaleFactor) * 4, space: colorSpace, bitmapInfo: bitmapInfo) else {
return
}
context.scaleBy(x: scaleFactor, y: scaleFactor)
context.concatenate(CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height))
UIGraphicsPushContext(context)
let strHeight = attrStr.boundingRect(with: size, options: .usesLineFragmentOrigin, context: nil).height
let yOffset = (size.height - strHeight)/2.0
attrStr.draw(with: CGRect(x: 0, y: yOffset, width: size.width, height: strHeight), options: .usesLineFragmentOrigin, context: nil)
if let imageRef = context.makeImage() {
texture = SKTexture(cgImage: imageRef)
} else {
texture = nil
}
UIGraphicsPopContext()
}
}
출처
2016-11-12 17:41:36
bzz
이 내가 ** ** 정적을지지 것 @MobileBen 동적 텍스트 또는 정적 –
에 대한인가, 다음, 어쩌면 할 수있는 방법을 찾아 동적 텍스트에 대해 작동하게하십시오. – Nik
예, 정적 텍스트 콘텐츠입니다.하지만 그로 인해 최종 비트 맵이 이동하고 크기가 조정됩니다. – Confused