UILabel发光字体
生活随笔
收集整理的這篇文章主要介紹了
UILabel发光字体
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、新建一個基于UILabel的子類
2、在這個子類中定義red、green、blue三個顏色值變量和一個發(fā)光光暈范圍變量size。
3、復(fù)寫UILable的drawTextInRect方法,并使用CGContextRef來進(jìn)行光暈效果繪制。
基本思路就是這樣子了,下面是部分主要代碼:
.h文件
//創(chuàng)建UILable的子類 GlowLable @interface GlowLabel : UILabel//定義顏色值全局變量和放大值全局變量 @property(assign,nonatomic)float redValue; @property(assign,nonatomic)float greenValue; @property(assign,nonatomic)float blueValue; @property(assign,nonatomic)float size;@end
.m文件
@implementation GlowLabel@synthesize redValue; @synthesize greenValue; @synthesize blueValue; @synthesize size;-(id) initWithFrame: (CGRect)frame {if ((self = [super initWithFrame:frame])) {//變量初始化redValue = 0.0f;greenValue = 0.50f;blueValue = 1.0f;size=20.0f;}return self; }//重寫UILable類的drawTextInRect方法 -(void) drawTextInRect: (CGRect)rect {//定義陰影區(qū)域CGSize textShadowOffest = CGSizeMake(0, 0);//定義RGB顏色值float textColorValues[] = {redValue, greenValue, blueValue, 1.0};//獲取繪制上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//保存上下文狀態(tài)CGContextSaveGState(ctx);//為上下文設(shè)置陰影CGContextSetShadow(ctx, textShadowOffest, size);//設(shè)置顏色類型CGColorSpaceRef textColorSpace = CGColorSpaceCreateDeviceRGB();//根據(jù)顏色類型和顏色值創(chuàng)建CGColorRef顏色CGColorRef textColor = CGColorCreate(textColorSpace, textColorValues);//為上下文陰影設(shè)置顏色,陰影顏色,陰影大小CGContextSetShadowWithColor(ctx, textShadowOffest, size, textColor);[super drawTextInRect:rect];//釋放CGColorRelease(textColor);CGColorSpaceRelease(textColorSpace);//重啟上下文CGContextRestoreGState(ctx); }@end
最終效果:
總結(jié)
以上是生活随笔為你收集整理的UILabel发光字体的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019中兴通讯软件开发岗c/c++方向
- 下一篇: java csv 双引号_你知道csv中