Supports the Label of the glow effect and the Label of the glow effect.
Supports the Label of the glow effect.
Effect
Source code
FBGlowLabel in a https://github.com/YouXianMing/UI-Component-Collection
//// FBGlowLabel. h /// Created by YouXianMing on 16/8/3. // Copyright©2016 YouXianMing. all rights reserved. //// https://github.com/lyokato/fbglowlabel//#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 /// Created by YouXianMing on 16/8/3. // Copyright©2016 YouXianMing. all rights reserved. // # 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. innerGlowSi Ze = 0.0f; self. innerGlowColor = [UIColor clearColor];}-(void) drawTextInRectForIOS7 :( CGRect) rect {CGContextRef ctx = UIGraphicsGetCurrentContext (); reset (rect. size, NO, 0.0); [super drawTextInRect: rect]; UIImage * textImage = upper (); 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) {uigraphicsbeginimagecontextwitexceptions (rect. size, NO, 0.0); CGContextRef ctx2 = UIGraphicsGetCurrentContext (); CGContextSaveGState (ctx2); CGContextSetFillColorWithColor (ctx2, [UIColor blackColor]. CGCo Lor); 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 = reverse (); CGContextClearRect (ctx2, rect); reverse (ctx2); reverse (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 = UIGraphicsGetImageFromCurr EntImageContext (); 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]; CGContext RestoreGState (ctx); if (_ innerGlowSize> 0) {uigraphicsbeginimagecontextwitexceptions (rect. size, NO, 0.0); CGContextRef ctx2 = UIGraphicsGetCurrentContext (); [super drawTextInRect: rect]; UIImage * textImage = histogram (); CGContextClearRect (ctx2, rect ); CGContextSaveGState (ctx2); CGContextSetFillColorWithColor (ctx2, [UIColor blackColor]. CGColor); CGContextFillRect (ctx 2, 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 = reverse (); CGContextClearRect (ctx2, rect); reverse (ctx2); reverse (ctx2, _ innerGlowColor. CGColor); C GContextSetShadowWithColor (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 (); UIGraphi CsEndImageContext (); [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
/// ViewController. m // FBGlowLabel /// Created by YouXianMing on 16/8/3. // Copyright©2016 YouXianMing. all rights reserved. // # import "ViewController. h "# import" FBGlowLabel. h "@ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. view. backgroundColor = [UIColor blackColor]; 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 cyanColor] colorWithAlphaComponent: 0.95f]; glowLabel. glowSize = 6; glowLabel. glowColor = [UIColor cyanColor]; glowLabel. innerGlowSize = 3; glowLabel. innerGlowColor = [[UIColor blackColor] colorWithAlphaComponent: 0.25f];} @ end