Skip to content

Instantly share code, notes, and snippets.

@kohashi
Created November 1, 2012 03:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohashi/3991482 to your computer and use it in GitHub Desktop.
Save kohashi/3991482 to your computer and use it in GitHub Desktop.
CCLabelTTFWithStroke : Label with Stroke(Shadow) for Cocos2d
//
// CCLabelTTFWithStroke.h
//
// Created by Kohashi on 2012/10/31.
// Copyright 2012年 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface CCLabelTTFWithStroke : CCLabelTTF {
}
@property (nonatomic) int stokeSize;
@property (nonatomic)ccColor3B strokeColor;
+(CCRenderTexture*) createStroke: (CCLabelTTF*) label size:(float)size color:(ccColor3B)cor;
@end
//
// CCLabelTTFWithStroke.m
// zoffy
//
// Created by Daisuke Kohashi on 2012/10/31.
// Copyright 2012年 __MyCompanyName__. All rights reserved.
//
#import "CCLabelTTFWithStroke.h"
@implementation CCLabelTTFWithStroke
@synthesize stokeSize = _stokeSize;
@synthesize strokeColor = _strokeColor;
#define kTagStroke 1029384756
+(CCRenderTexture*) createStroke: (CCLabelTTF*) label size:(float)size color:(ccColor3B)cor
{
CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:label.texture.contentSize.width+size*2 height:label.texture.contentSize.height+size*2];
CGPoint originalPos = [label position];
ccColor3B originalColor = [label color];
float originalScaleX = [label scaleX];
float originalScaleY = [label scaleY];
BOOL originalVisibility = [label visible];
[label setColor:cor];
[label setScale:1];
[label setVisible:YES];
ccBlendFunc originalBlend = [label blendFunc];
[label setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE }];
CGPoint bottomLeft = ccp(label.texture.contentSize.width * label.anchorPoint.x + size, label.texture.contentSize.height * label.anchorPoint.y + size);
CGPoint positionOffset= ccp(label.contentSize.width/2, label.contentSize.height/2);
[rt begin];
for (int i=0; i<360; i+=45) // you should optimize that for your needs
{
[label setPosition:ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)];
[label visit];
}
[rt end];
[label setPosition:originalPos];
[label setColor:originalColor];
[label setBlendFunc:originalBlend];
[label setVisible:originalVisibility];
[label setScaleX: originalScaleX];
[label setScaleY: originalScaleY];
rt.position = positionOffset;
return rt;
}
//----------
-(id)init{
id my = [super init];
_stokeSize = 2;
_strokeColor = ccc3(15, 15, 15);
return my;
}
-(void)setFontName:(NSString *)fontName{
[super setFontName:fontName];
[self setShadow];
}
-(void)setFontSize:(float)fontSize{
[super setFontSize:fontSize];
[self setShadow];
}
-(void) setString:(NSString *)string
{
[super setString:string];
[self setShadow];
}
-(void)setOpacity:(GLubyte)opacity{
[super setOpacity:opacity];
[self setShadow];
}
-(void) setShadow
{
[self removeChildByTag:kTagStroke cleanup:YES];
if(!self.string) return;
CCRenderTexture * stroke = [CCLabelTTFWithStroke createStroke:(CCLabelTTF*)self size:_stokeSize color:_strokeColor];
[self addChild:stroke z:-1 tag:kTagStroke];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment