iOS动画——辉光效果(Label)
来源:互联网 发布:西游之路升阶数据 编辑:程序博客网 时间:2024/06/10 04:38
一, 效果图如下:
二, 代码如下
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开发 刘成利"; 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];
0 0
- iOS动画——辉光效果(Label)
- 辉光效果(Glow Effect)
- GraphicsLab Project之辉光(Glare,Glow)效果
- IOS——各种动画效果
- iOS—动画效果和实现
- IOS小技巧——如何润色一个Label, 一个label中,展现多种字体效果(图文混编 1)
- iOS——自适应label
- iOS —label自动换行
- iOS —label自动换行
- iOS项目开发实战——视图动画效果
- iOS造轮子——蚱蜢加载动画效果
- iOS——各种图片、界面变化的动画效果
- 使用threejs实现辉光&大气层效果
- iOS开发之UI篇(6)—— 动画效果
- iOS UIView animation动画效果 (1)
- ios各种动画效果
- IOS 动画效果
- IOS 动画效果
- ios开发--在Swift中使用JavaScript的方法和技巧
- 《iOS 6 Programming Cookbook》读书笔记
- spark 2.0 SparkListener 继承关系
- 测试自动化--学到的10课(血泪史)
- 【iOS知识学习】_int、NSInteger、NSUInteger、NSNumber的区别和联系
- iOS动画——辉光效果(Label)
- [LeetCode]60. Permutation Sequence
- hadoop添加snappy解压缩库
- sage安装和使用
- xml文件中的变量参数
- 欢迎使用CSDN-markdown编辑器
- Android luancher消息提醒-ShortcutBadger
- shell下获取上一个月,星期时间和时间戳的范围
- NO.1_461. Hamming Distance