iOS mask 遮罩,彩色文本
xxxixxxx

iOS 渐变彩色文字、iOS mask 遮罩

效果图

使用到了 YYCategories

彩色文字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
UIView *colorBgView = [UIView new];
[self.view addSubview:colorBgView];
colorBgView.frame = CGRectMake(0, 300, UIScreen.mainScreen.bounds.size.width, 40);

//这里用了YYCategories
UIBezierPath *colorTextPath = [UIBezierPath bezierPathWithText:@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈" font:[UIFont systemFontOfSize:28]];
CAShapeLayer *colorTextLayer = [CAShapeLayer layer];
colorTextLayer.path = colorTextPath.CGPath;
colorTextLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 30);
colorBgView.layer.mask = colorTextLayer;

CAGradientLayer *colorLayer = CAGradientLayer.new;
colorLayer.colors = @[(__bridge id)UIColor.redColor.CGColor,
(__bridge id)UIColor.orangeColor.CGColor,
(__bridge id)UIColor.greenColor.CGColor,
(__bridge id)UIColor.blueColor.CGColor,
(__bridge id)UIColor.yellowColor.CGColor,
(__bridge id)UIColor.purpleColor.CGColor,
(__bridge id)UIColor.blackColor.CGColor,];
colorLayer.startPoint = CGPointMake(0, 0.5);
colorLayer.endPoint = CGPointMake(1, 0.5);
colorLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 40);
[colorBgView.layer addSublayer:colorLayer];

过渡遮罩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

///黑色背景view
UIView *indicatorView = [UIView new];
[self.view addSubview:indicatorView];
indicatorView.backgroundColor = UIColor.blackColor;
indicatorView.layer.cornerRadius = 20;
indicatorView.layer.masksToBounds = YES;
indicatorView.frame = CGRectMake(0, 195, 100, 40);
self.indicatorView = indicatorView;

//这里用了YYCategories
UIBezierPath *textPath = [UIBezierPath bezierPathWithText:@"哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈" font:[UIFont systemFontOfSize:28]];
CAShapeLayer *textLayer = [CAShapeLayer layer];
textLayer.path = textPath.CGPath;
textLayer.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 30);


UIView *darkView = [UIView new];
[self.view addSubview:darkView];
darkView.backgroundColor = UIColor.darkGrayColor;
darkView.frame = CGRectMake(0, 200, UIScreen.mainScreen.bounds.size.width, 30);
darkView.layer.mask = textLayer;


UIView *whiteView = [UIView new];
[darkView addSubview:whiteView];
whiteView.backgroundColor = UIColor.whiteColor;
whiteView.layer.cornerRadius = 20;
whiteView.layer.masksToBounds = YES;
whiteView.frame = CGRectMake(0, -5, 100, 40);
self.whiteView = whiteView;

//也可以使用layer
// CALayer *darkLayer = CALayer.new;
// [self.view.layer addSublayer:darkLayer];
// darkLayer.backgroundColor = UIColor.darkGrayColor.CGColor;
// darkLayer.frame = CGRectMake(0, 200, UIScreen.mainScreen.bounds.size.width, 30);
// darkLayer.mask = textLayer;
//
// CAShapeLayer *whiteLayer = CAShapeLayer.new;
// [darkLayer addSublayer:whiteLayer];
// whiteLayer.backgroundColor = UIColor.whiteColor.CGColor;
// whiteLayer.fillColor = UIColor.whiteColor.CGColor;
// whiteLayer.strokeColor = UIColor.whiteColor.CGColor;
// whiteLayer.borderColor = UIColor.whiteColor.CGColor;
// whiteLayer.cornerRadius = 20;
// whiteLayer.masksToBounds = YES;
// whiteLayer.frame = CGRectMake(0, -5, 100, 40);
// self.whiteLayer = whiteLayer;

UISlider *slider = UISlider.new;
[self.view addSubview:slider];
[slider addTarget:self action:@selector(changeFrame:) forControlEvents:UIControlEventValueChanged];
slider.frame = CGRectMake(0, 260, UIScreen.mainScreen.bounds.size.width, 30);


----------

- (void)changeFrame:(UISlider *)slider {

CGFloat x = UIScreen.mainScreen.bounds.size.width *slider.value;
self.indicatorView.frame = CGRectMake(x, 195, 100, 40);
self.whiteView.frame = CGRectMake(x, -5, 100, 40);
self.whiteLayer.frame = CGRectMake(x, -5, 100, 40);

}

  • Post title:iOS mask 遮罩,彩色文本
  • Post author:xxxixxxx
  • Create time:2020-11-20 00:00:00
  • Post link:https://xxxixxx.github.io/2020/11/20/500-iOS渐变彩色文字、iOS-mask-遮罩/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
 Comments