From 25ca5a1acba5b5c16eb6de15a046d4675ad2b74f Mon Sep 17 00:00:00 2001 From: Peter Easdown Date: Tue, 29 Nov 2016 08:20:50 +1100 Subject: [PATCH 1/2] Added Objective-C port of ASAttributedLabelNode --- ASAttributedLabelNode.h | 16 +++++++++++ ASAttributedLabelNode.m | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 ASAttributedLabelNode.h create mode 100644 ASAttributedLabelNode.m diff --git a/ASAttributedLabelNode.h b/ASAttributedLabelNode.h new file mode 100644 index 0000000..f4aebad --- /dev/null +++ b/ASAttributedLabelNode.h @@ -0,0 +1,16 @@ +// +// ASAttributedLabelNode.h +// +// Created by Peter Easdown on 27/11/16. +// Inspired by Alex Studnicka +// + +#import + +@interface ASAttributedLabelNode : SKSpriteNode + +@property (nonatomic, retain) NSAttributedString *attributedText; + +- (id) initWithSize:(CGSize)size; + +@end diff --git a/ASAttributedLabelNode.m b/ASAttributedLabelNode.m new file mode 100644 index 0000000..fcf25b9 --- /dev/null +++ b/ASAttributedLabelNode.m @@ -0,0 +1,61 @@ +// +// ASAttributedLabelNode.h +// +// Created by Peter Easdown on 27/11/16. +// Inspired by Alex Studnicka +// + +#import "ASAttributedLabelNode.h" + +@implementation ASAttributedLabelNode + +- (id) initWithSize:(CGSize)size { + self = [super initWithColor:[UIColor clearColor] size:size]; + + return self; +} + +- (void) setAttributedText:(NSAttributedString *)attributedText { + _attributedText = attributedText; + + [self draw]; +} + +- (void) draw { + NSAttributedString *attrStr = self.attributedText; + + if (attrStr == nil) { + self.texture = nil; + + return; + } + + float scaleFactor = [UIScreen mainScreen].scale; + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + CGImageAlphaInfo bitmapInfo = kCGImageAlphaPremultipliedLast; + CGContextRef context = CGBitmapContextCreate(nil, self.size.width * scaleFactor, self.size.height * scaleFactor, 8, (self.size.width * scaleFactor) * 4, colorSpace, bitmapInfo); + + if (context == nil) { + return; + } + + CGContextScaleCTM(context, scaleFactor, scaleFactor); + CGContextConcatCTM(context, CGAffineTransformMake(1, 0, 0, -1, 0, self.size.height)); + UIGraphicsPushContext(context); + + float strHeight = [attrStr boundingRectWithSize:self.size options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height; + float yOffset = (self.size.height - strHeight) / 2.0; + CGRect rect = CGRectMake(0.0, yOffset, self.size.width, strHeight); + [attrStr drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin context:nil]; + + CGImageRef imageRef = CGBitmapContextCreateImage(context); + + if (imageRef != nil) { + self.texture = [SKTexture textureWithCGImage:imageRef]; + CGImageRelease(imageRef); + } + + UIGraphicsPopContext(); +} + +@end From a109770c5305c1fe7f7fe4ae6b24ef5f22f76d51 Mon Sep 17 00:00:00 2001 From: Peter Easdown Date: Fri, 9 Dec 2016 08:00:52 +1100 Subject: [PATCH 2/2] =?UTF-8?q?The=20calculation=20of=20the=20number=20of?= =?UTF-8?q?=20bytes=20per=20row=20didn=E2=80=99t=20work=20properly=20on=20?= =?UTF-8?q?older=20devices=20(e.g.=20iPad2).=20=20Changed=20to=20allow=20C?= =?UTF-8?q?GBitmapContextCreate=20to=20calculate=20it=20properly=20interna?= =?UTF-8?q?lly.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ASAttributedLabelNode.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ASAttributedLabelNode.m b/ASAttributedLabelNode.m index fcf25b9..f6c0cb2 100644 --- a/ASAttributedLabelNode.m +++ b/ASAttributedLabelNode.m @@ -33,7 +33,7 @@ - (void) draw { float scaleFactor = [UIScreen mainScreen].scale; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGImageAlphaInfo bitmapInfo = kCGImageAlphaPremultipliedLast; - CGContextRef context = CGBitmapContextCreate(nil, self.size.width * scaleFactor, self.size.height * scaleFactor, 8, (self.size.width * scaleFactor) * 4, colorSpace, bitmapInfo); + CGContextRef context = CGBitmapContextCreate(nil, self.size.width * scaleFactor, self.size.height * scaleFactor, 8, 0, colorSpace, bitmapInfo); if (context == nil) { return;