//
// GlowLabel.h
// 帅哥句子
//
// Created by beyond on 16/8/5.
// Copyright © 2016年 beyond. All rights reserved.
// 发光的Label
#import <UIKit/UIKit.h>
@interface GlowLabel : UILabel
//定义颜色值全局变量和放大值全局变量
@property(assign,nonatomic)float redValue;
@property(assign,nonatomic)float greenValue;
@property(assign,nonatomic)float blueValue;
@property(assign,nonatomic)float size;
@end
//
// GlowLabel.m
// 帅哥句子
//
// Created by beyond on 16/8/5.
// Copyright © 2016年 beyond. All rights reserved.
//
#import "GlowLabel.h"
@implementation GlowLabel
@synthesize redValue;
@synthesize greenValue;
@synthesize blueValue;
@synthesize size;
-(id) initWithFrame: (CGRect)frame {
if ((self = [super initWithFrame:frame])) {
//变量初始化
redValue = 1.0f;
greenValue = 1.0f;
blueValue = 1.0f;
size=8.0f;//20.0f;
}
return self;
}
//重写UILable类的drawTextInRect方法
-(void) drawTextInRect: (CGRect)rect {
//定义阴影区域
CGSize textShadowOffest = CGSizeMake(0, 0);
//定义RGB颜色值
CGFloat 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