標籤:
+ (UIColor*) gradientFromColor:(UIColor*)c1 toColor:(UIColor*)c2 withWidth:(CGFloat)width andHeight:(CGFloat)height{ CGSize size = CGSizeMake(width, height); UIGraphicsBeginImageContextWithOptions(size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); NSArray* colors = [NSArray arrayWithObjects:(id)c1.CGColor, (id)c2.CGColor, nil]; CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (CFArrayRef)colors, NULL); CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(size.width, size.height), 0); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGGradientRelease(gradient); CGColorSpaceRelease(colorspace); UIGraphicsEndImageContext(); return [UIColor colorWithPatternImage:image];}
比如給一個UILabel設定了漸層顏色的效果
設定漸層背景色
設定簡便字型色
iOS為UIView的子類添加漸層顏色