从上至下依次为:CCLabelTTF阴影效果、CCLabelTTF描边和UILabel描边。
CCLabelTTF阴影:
其实现并不是简简单单添加的两层文字加上一点偏移。
CCLabelFX *label1 = [CCLabelFX labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:40 shadowOffset:CGSizeMake(2, -2) shadowBlur:1.0f shadowColor:ccc4(255, 255, 255, 255) fillColor:ccc4(255, 0, 0, 255)];
label1.position = ccp( size.width /2 , size.height/2 + 80 );
[self addChild:label1];
shadowOffset是偏移量,shadowBlur是阴影的透明度,shadowColor是阴影颜色,fillColor是字体颜色。
参考文章: http://www.cocos2d-iphone.org/forum/topic/13798
Class下载: https://github.com/jandrad/CCLabelFX
CCLabelTTF描边:
主要代码
+(CCRenderTexture *)createStroke:(CCLabelTTF *)label size:(float)size color:(ccColor3B)cor
{
CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:label.contentSize.width+size*2 height:label.contentSize.height+size*2];
CGPoint originalPos = [label position];
ccColor3B originalColor = [label color];
BOOL originalVisibility = [label visible];
[label setColor:cor];
[label setVisible:YES];
ccBlendFunc originalBlend = [label blendFunc];
[label setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE }];
CGPoint bottomLeft = ccp(label.contentSize.width * label.anchorPoint.x + size, label.contentSize.height * label.anchorPoint.y + size);
CGPoint positionOffset = ccp(label.contentSize.width * label.anchorPoint.x – label.contentSize.width/2,label.contentSize.height * label.anchorPoint.y – label.contentSize.height/2);
CGPoint position = ccpSub(originalPos, positionOffset);
[rt begin];
for (int i=0; i<360; i+=30) // 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];
[rt setPosition:position];
return rt;
}
使用方法
CCLabelTTF *label = [CCLabelTTF labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
label.color = ccRED;
label.position = ccp( size.width /2 , size.height/2 );
CCRenderTexture *stroke = [Common createStroke:label size:3 color:ccWHITE];
[self addChild:stroke];
[self addChild: label];
参考文章: http://www.cocos2d-iphone.org/forum/topic/12126
里面还有关于CCMenuItemFont描边的实现方法。
UIColor描边:
新建一个Class继承UILabel,重写其drawTextInRect方法即可。
-(void)drawTextInRect:(CGRect)rect
{
if (!strokeColor) {//边线色值
strokeColor = [UIColor whiteColor];
}
if (strokeSize == 0) {//边线宽度
strokeSize = 1;
}
CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, strokeSize);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = strokeColor;
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
self.shadowOffset = shadowOffset;
}
二、普通空心文字(边线颜色宽度均可调整)
//.h
#import <UIKit/UIKit.h>
@interface StrokeLabel : UILabel
@property(nonatomic, retain)UIColor* strokeColor;//边框颜色
@property(nonatomic, assign)float strokeSize;//边框宽度
@end
//.m
#import "StrokeLabel.h"
@implementation StrokeLabel
-(void)drawTextInRect:(CGRect)rect{
if (!self.strokeColor) {
self.strokeColor = [UIColor redColor];
}
if (self.strokeSize == 0) {
self.strokeSize = 1;
}
CGSize shadowOffset = self.shadowOffset;
UIColor *textColor = self.textColor;
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(c, self.strokeSize);
CGContextSetLineJoin(c, kCGLineJoinRound);
CGContextSetTextDrawingMode(c, kCGTextStroke);
self.textColor = self.strokeColor;
[super drawTextInRect:rect];
CGContextSetTextDrawingMode(c, kCGTextFill);
self.textColor = textColor;
self.shadowOffset = CGSizeMake(0, 0);
[super drawTextInRect:rect];
self.shadowOffset = shadowOffset;
}
@end