ios开发之文字扩散效果
#import <UIKit/UIKit.h>
@interface LightLabel : UILabel{
float redValue_;
float greenValue_;
float blueValue_;
float size_;
}
@property(assign,nonatomic)float redValue;
@property(assign,nonatomic)float greenValue;
@property(assign,nonatomic)float blueValue;
@property(assign,nonatomic)float size;
@end
#import "LightLabel.h"
@implementation LightLabel
@synthesize redValue = redValue_, greenValue = greenValue_, blueValue = blueValue_, size = size_;
-(void)awakeFromNib{
[super awakeFromNib];
// 6 238 238
redValue_ = 6.0/255.0f;
greenValue_ = 238.0/255.0f;
blueValue_ = 238.0/255.0f;
size_ = 2.0f;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
redValue_ = 6.0/255.0f;
greenValue_ = 238.0/255.0f;
blueValue_ = 238.0/255.0f;
size_ = 2.0f;
}
return self;
}
-(void) drawTextInRect: (CGRect)rect{
//定义阴影区域
CGSize textShadowOffest = CGSizeMake(0, 0);
//定义RGB颜色值
float textColorValues[] = {redValue_, greenValue_, blueValue_, 1.0};
//获取绘制上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//保存上下文状态
CGContextSaveGState(ctx);
//为上下文设置阴影
CGContextSetShadow(ctx, textShadowOffest, size_);
//设置颜色类型
CGColorSpaceRef textColorSpace = CGColorSpaceCreateDeviceRGB();
//根据颜色类型和颜色值创建CGColorRef颜色
CGColorRef textColor = CGColorCreate(textColorSpace, textColorValues);
//为上下文阴影设置颜色,阴影颜色,阴影大小
CGContextSetShadowWithColor(ctx, textShadowOffest, size_, textColor);
[super drawTextInRect:rect];
//释放
CGColorRelease(textColor);
CGColorSpaceRelease(textColorSpace);
//重启上下文
CGContextRestoreGState(ctx);
}
@end