iOS渐变色Label文字

必须在UILabel绘制后执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIColor *color1 = HEXCOLOR(0xfc7d48);//[UIColor colorWithRed:255/255 green:57/255 blue:77/255 alpha:1];
UIColor *color2 = HEXCOLOR(0xfc394d);//[UIColor colorWithRed:252/255 green:125/255 blue:72/255 alpha:1];
self.gradientLayer = [CAGradientLayer layer];
//渐变色位置
self.gradientLayer.startPoint = CGPointMake(0.0,1.0);
self.gradientLayer.endPoint = CGPointMake(1.0, 1.0);
self.gradientLayer.locations = @[@(0),@(0.3),@(1.0)];

self.gradientLayer.frame = self.colorLab.frame;
//渐变色颜色
self.gradientLayer.colors = @[(id)color1.CGColor,(id)color2.CGColor];
[self.channelNameLab.layer addSublayer:self.gradientLayer];
self.gradientLayer.mask = self.colorLab.layer;
self.colorLab.frame = self.gradientLayer.bounds;
});

本文总阅读量