UILabel设置字体发光效果

来源:互联网 发布:制作歌词软件 编辑:程序博客网 时间:2024/05/16 05:52

1、新建一个继承自UILabel的类
2、在这个类中定义red、green、blue三个颜色值变量和一个发光范围变量glowSize。
3、重写UILable的drawTextInRect方法,并使用CGContextRef来进行绘制。

.h文件

@interface FBGlowLabel : UILabel  //定义颜色值全局变量和放大值全局变量  @property(assign ,nonatomic) float red;  @property(assign ,nonatomic) float green;  @property(assign ,nonatomic) float blue;  @property(assign ,nonatomic) float glowSize;  

.m文件

@implementation FBGlowLabel  -(id) initWithFrame: (CGRect)frame {      if ((self = [super initWithFrame:frame])) {          //初始化          red = 0.0f;          green = 0.50f;          blue = 1.0f;          glowSize=40.0f;      }      return self;  }  //重写UILable类的drawTextInRect方法  -(void) drawTextInRect: (CGRect)rect {      //定义阴影区域      CGSize textShadowOffest = CGSizeMake(0, 0);      //定义RGB颜色值      float textColors[] = {red, green, blue, 1.0};      //获取绘制上下文      CGContextRef ctx = UIGraphicsGetCurrentContext();      //保存上下文状态      CGContextSaveGState(ctx);      //为上下文设置阴影      CGContextSetShadow(ctx, textShadowOffest, glowSize);      //设置颜色类型      CGColorSpaceRef textColorSpace = CGColorSpaceCreateDeviceRGB();      //根据颜色类型和颜色值创建CGColorRef颜色      CGColorRef textColor = CGColorCreate(textColorSpace, textColors);      //为上下文阴影设置颜色,阴影颜色,阴影大小      CGContextSetShadowWithColor(ctx, textShadowOffest, size, textColor);      [super drawTextInRect:rect];      //释放      CGColorRelease(textColor);      CGColorSpaceRelease(textColorSpace);      //重启上下文      CGContextRestoreGState(ctx);  }  
0 0
原创粉丝点击