iOS Label实现文字渐变色效果

时间:2021-05-20

前言

前一段时间公司有需求做文字的的渐变色,自己当时也是网上看了一些,自己写了两个方法,实现了需求,写了很久了,只是现在才想起来,就当继续学习了。分享出来供大家参考学习,下面来看看详细的介绍:

先看看简单的:

- (void)addGradientRampWithColors:(NSArray *)colors text:(NSString *)text { //label在父视图上的(x,y)的值不是中心点 CGPoint point = CGPointMake(30, 500); UILabel *label = [[UILabel alloc]init]; label.text = text; label.font = [UIFont systemFontOfSize:20]; // label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; //label的中心和想象的一样啦!! label.center = CGPointMake(point.x + CGRectGetWidth(label.bounds)/2, point.y - CGRectGetHeight(label.bounds)/2); [self.view addSubview:label]; //这个label是和上面的label是对齐的哦,之前都不好对齐,用这样的方法设置frame就好了// UILabel *infoTextLabel = [[UILabel alloc] init];// infoTextLabel.frame = CGRectMake(label.center.x - CGRectGetWidth(label.bounds)/2 ,point.y + 30, 220, 50);// infoTextLabel.text = @"你说的是哦";// infoTextLabel.font = [UIFont systemFontOfSize:20];// infoTextLabel.backgroundColor =[UIColor redColor];// infoTextLabel.numberOfLines = 0;// infoTextLabel.textAlignment = NSTextAlignmentLeft;// infoTextLabel.textColor = [UIColor blueColor];// [infoTextLabel sizeToFit];// [self.view addSubview:infoTextLabel]; //在后面添加渐变图层 CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = label.frame; gradientLayer.colors = colors; //渐变的方向(0,0) (0,1) (1,0)(1,1)为四个顶点方向 //(I.e. [0,0] is the bottom-left // corner of the layer, [1,1] is the top-right corner.) The default values // are [.5,0] and [.5,1] gradientLayer.startPoint = CGPointMake(0, 1); gradientLayer.endPoint = CGPointMake(1, 1); [self.view.layer addSublayer:gradientLayer]; gradientLayer.mask = label.layer; label.frame = gradientLayer.bounds;}

自己觉得这样的方法用起来不是很方便,所以接下来是另一种方法:

.m文件

@implementation CFGradientLabel- (void)drawRect:(CGRect)rect { CGSize textSize = [self.text sizeWithAttributes:@{NSFontAttributeName : self.font}]; CGRect textRect = (CGRect){0, 0, textSize}; // 画文字(不做显示用, 主要作用是设置 layer 的 mask) CGContextRef context = UIGraphicsGetCurrentContext(); [self.textColor set]; [self.text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:NULL]; // 坐标(只对设置后的画到 context 起作用, 之前画的文字不起作用) CGContextTranslateCTM(context, 0.0f, rect.size.height - (rect.size.height - textSize.height) * 0.5); CGContextScaleCTM(context, 1.0f, -1.0f); CGImageRef alphaMask = CGBitmapContextCreateImage(context); CGContextClearRect(context, rect); // 清除之前画的文字 // 设置mask CGContextClipToMask(context, rect, alphaMask); // 画渐变色 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, NULL); CGPoint startPoint = CGPointMake(textRect.origin.x, textRect.origin.y); CGPoint endPoint = CGPointMake(textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); // 释放内存 CGColorSpaceRelease(colorSpace); CGGradientRelease(gradient); CFRelease(alphaMask);}

.h 文件

@interface CFGradientLabel : UILabel@property(nonatomic, strong) NSArray* colors;@end

接下来是调用的方法,修改了一下的

- (void)addGradientLabelWithFrame:(CGPoint)point gradientText:(NSString *)text infoText:(NSString *)infoText colors:(NSArray *)colors font:(UIFont *)font { static NSInteger labelTag = 200; CFGradientLabel *lable = [[CFGradientLabel alloc] init]; lable.text = text; lable.font = font; lable.tag = labelTag; lable.textAlignment = NSTextAlignmentCenter; [lable sizeToFit]; //之前项目的时候设置了为0,忘了注释,所以有的小伙伴用的时候就不显示了……(^-^)// lable.alpha = 0; lable.center = point; lable.colors = colors; [self.view addSubview:lable];}

做的是引导页,看看效果图如下:


总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章