您的位置:首页 > 移动开发 > IOS开发

iOS_带发光效果的Label

2016-08-05 16:35 531 查看
//
// 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 发光 Label