UIview子类设置渐变色

UIview子类设置渐变色

// 前置backgroundColor需要设置为clearColor
_titleLabel.backgroundColor = [UIColor ClearColor];
// UIview子类设置渐变色
_titleLabel.backgroundColor = [UIColor clearColor];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(0, 0, 375, 40);
gradient.colors = [NSArray arrayWithObjects:
            (id)[UIColor redColor].CGColor,
            (id)[UIColor blackColor].CGColor,
            (id)[UIColor yellowColor].CGColor, nil];
gradient.startPoint = CGPointMake(0.5, 0);
gradient.endPoint = CGPointMake(0.5, 1);
gradient.locations = @[@0.0, @0.5, @1.0];
[_titleLabel.layer addSublayer:gradient];

你可能感兴趣的:(UIview子类设置渐变色)