iOS學(xué)習(xí)之iOS動畫——輝光效果(Label)

一:效果圖如下:

20170112184959106.png

二:代碼如下

FBGlowLabel.h

#import <UIKit/UIKit.h>
 
@interface FBGlowLabel : UILabel
 
/**
 *  Glow size, default is 0.f.
 */
@property (nonatomic) CGFloat glowSize;
 
/**
 *  Glow color, default is clear color.
 */
@property (nonatomic, strong) UIColor *glowColor;
 
/**
 *  Inner glow size, default is 0.f.
 */
@property (nonatomic) CGFloat innerGlowSize;
 
/**
 *  Inner glow color, default is clear color.
 */
@property (nonatomic, strong) UIColor *innerGlowColor;

@end

FBGlowLabel.m

#import "FBGlowLabel.h"
 
@implementation FBGlowLabel
 
- (id)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
    }
    
    return self;
}
 
- (id)initWithCoder:(NSCoder *)coder {
    
    if (self = [super initWithCoder:coder]) {
        
        [self setup];
    }
    
    return self;
}
 
- (void)setup {
    
    self.glowSize       = 0.0f;
    self.glowColor      = [UIColor clearColor];
    
    self.innerGlowSize  = 0.0f;
    self.innerGlowColor = [UIColor clearColor];
}
 
- (void)drawTextInRectForIOS7:(CGRect)rect {
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
    
    [super drawTextInRect:rect];
    UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGContextSaveGState(ctx);
    
    if (_glowSize > 0) {
        
        CGContextSetShadow(ctx, CGSizeZero, _glowSize);
        CGContextSetShadowWithColor(ctx, CGSizeZero, _glowSize, _glowColor.CGColor);
    }
    
    [textImage drawAtPoint:rect.origin];
    CGContextRestoreGState(ctx);
    
    if (_innerGlowSize > 0) {
 
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        CGContextRef ctx2 = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, [UIColor blackColor].CGColor);
        CGContextFillRect(ctx2, rect);
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, textImage.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *inverted = UIGraphicsGetImageFromCurrentImageContext();
        CGContextClearRect(ctx2, rect);
        
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, _innerGlowColor.CGColor);
        CGContextSetShadowWithColor(ctx2, CGSizeZero, _innerGlowSize, _innerGlowColor.CGColor);
        [inverted drawAtPoint:CGPointZero];
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, inverted.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *innerShadow = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        [innerShadow drawAtPoint:rect.origin];
    }
}
 
- (void)drawTextInRectForIOS6:(CGRect)rect {
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    
    if (self.glowSize > 0) {
        
        CGContextSetShadow(ctx, CGSizeZero, _glowSize);
        CGContextSetShadowWithColor(ctx, CGSizeZero, _glowSize, _glowColor.CGColor);
    }
    
    [super drawTextInRect:rect];
    CGContextRestoreGState(ctx);
    
    if (_innerGlowSize > 0) {
 
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
        
        CGContextRef ctx2 = UIGraphicsGetCurrentContext();
        [super drawTextInRect:rect];
        
        UIImage *textImage = UIGraphicsGetImageFromCurrentImageContext();
        CGContextClearRect(ctx2, rect);
        
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, [UIColor blackColor].CGColor);
        CGContextFillRect(ctx2, rect);
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, textImage.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *inverted = UIGraphicsGetImageFromCurrentImageContext();
        CGContextClearRect(ctx2, rect);
        
        CGContextSaveGState(ctx2);
        CGContextSetFillColorWithColor(ctx2, _innerGlowColor.CGColor);
        CGContextSetShadowWithColor(ctx2, CGSizeZero, _innerGlowSize, _innerGlowColor.CGColor);
        [inverted drawAtPoint:CGPointZero];
        CGContextTranslateCTM(ctx2, 0.0, rect.size.height);
        CGContextScaleCTM(ctx2, 1.0, -1.0);
        CGContextClipToMask(ctx2, rect, inverted.CGImage);
        CGContextClearRect(ctx2, rect);
        CGContextRestoreGState(ctx2);
        
        UIImage *innerShadow = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        [innerShadow drawAtPoint:rect.origin];
    }
}
 
- (void)drawTextInRect:(CGRect)rect {
    
    if (self.text == nil || self.text.length == 0) {
        
        return;
    }
    
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
        
        [self drawTextInRectForIOS7:rect];
        
    } else {
        
        [self drawTextInRectForIOS6:rect];
    }
}
 
@end

三:使用方法

 FBGlowLabel *glowLabel    = [[FBGlowLabel alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:glowLabel];
    
    glowLabel.text            = @"紅紅火火";
    glowLabel.textAlignment   = NSTextAlignmentCenter;
    glowLabel.backgroundColor = [UIColor clearColor];
    glowLabel.font            = [UIFont fontWithName:@"Heiti SC" size:40.f];
    glowLabel.textColor       = [[UIColor redColor] colorWithAlphaComponent:0.95f];
    
    glowLabel.glowSize       = 6;
    glowLabel.glowColor      = [UIColor cyanColor];
    
    glowLabel.innerGlowSize  = 3;
    glowLabel.innerGlowColor = [[UIColor blackColor] colorWithAlphaComponent:0.25f];
    
    
    
    FBGlowLabel *glowLabel2    = [[FBGlowLabel alloc] initWithFrame:CGRectMake(50, 500, 300, 40)];
    [self.view addSubview:glowLabel2];
    
    glowLabel2.text            = @"iOS開發(fā) 劉成利";
    glowLabel2.textAlignment   = NSTextAlignmentCenter;
    glowLabel2.backgroundColor = [UIColor clearColor];
    glowLabel2.font            = [UIFont fontWithName:@"Heiti SC" size:40.f];
    glowLabel2.textColor       = [[UIColor cyanColor] colorWithAlphaComponent:0.95f];
    
    glowLabel2.glowSize       = 6;
    glowLabel2.glowColor      = [UIColor cyanColor];
    
    glowLabel2.innerGlowSize  = 3;
    glowLabel2.innerGlowColor = [[UIColor blackColor] colorWithAlphaComponent:0.25f];

感謝:
https://blog.csdn.net/IT_liuchengli/article/details/54286752

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市翘地,隨后出現(xiàn)的幾起案子坑雅,更是在濱河造成了極大的恐慌吃谣,老刑警劉巖耗帕,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異吉嫩,居然都是意外死亡其掂,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進(jìn)店門扁耐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來暇检,“玉大人,你說我怎么就攤上這事婉称】槠停” “怎么了?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵王暗,是天一觀的道長悔据。 經(jīng)常有香客問我,道長俗壹,這世上最難降的妖魔是什么科汗? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮绷雏,結(jié)果婚禮上肛捍,老公的妹妹穿的比我還像新娘隐绵。我一直安慰自己,他們只是感情好拙毫,可當(dāng)我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布依许。 她就那樣靜靜地躺著,像睡著了一般缀蹄。 火紅的嫁衣襯著肌膚如雪峭跳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天缺前,我揣著相機與錄音蛀醉,去河邊找鬼。 笑死衅码,一個胖子當(dāng)著我的面吹牛拯刁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播逝段,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼垛玻,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了奶躯?” 一聲冷哼從身側(cè)響起帚桩,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎嘹黔,沒想到半個月后账嚎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡儡蔓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年郭蕉,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片喂江。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡恳不,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出开呐,到底是詐尸還是另有隱情,我是刑警寧澤规求,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布筐付,位于F島的核電站,受9級特大地震影響阻肿,放射性物質(zhì)發(fā)生泄漏瓦戚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一丛塌、第九天 我趴在偏房一處隱蔽的房頂上張望较解。 院中可真熱鬧畜疾,春花似錦、人聲如沸印衔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奸焙。三九已至瞎暑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間与帆,已是汗流浹背了赌。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留玄糟,地道東北人勿她。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓阵翎,卻偏偏與公主長得像逢并,于是被迫代替她去往敵國和親贮喧。 傳聞我的和親對象是個殘疾皇子筒狠,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容