label设置渐变色

//渐变色
CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
gradientLayer0.cornerRadius = 5;
gradientLayer0.frame = CGRectMake(0, 0, 41, 41);
gradientLayer0.colors = @[
    (id)[UIColor colorWithRed:217.0f/255.0f green:238.0f/255.0f blue:255.0f/255.0f alpha:1.0f].CGColor,
    (id)[UIColor colorWithRed:246.0f/255.0f green:251.0f/255.0f blue:255.0f/255.0f alpha:1.0f].CGColor];
gradientLayer0.locations = @[@0, @1];
[gradientLayer0 setStartPoint:CGPointMake(1, 1)];
[gradientLayer0 setEndPoint:CGPointMake(1, 0)];

//根据渐变色生成图片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(41, 41), YES, 0.0);
[gradientLayer0 renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//设置背景颜色
leftLabel.backgroundColor = [UIColor colorWithPatternImage:img];

你可能感兴趣的:(label设置渐变色)