Created
October 14, 2013 23:19
-
-
Save rnaud/6983920 to your computer and use it in GitHub Desktop.
UIColor gradient from two UIColors, to be used with a UILabel.textColor or a NSMutableAttributeString.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (UIColor*) gradientFromColor:(UIColor*)c1 toColor:(UIColor*)c2 withHeight:(int)height | |
{ | |
CGSize size = CGSizeMake(1, 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(0, size.height), 0); | |
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); | |
CGGradientRelease(gradient); | |
CGColorSpaceRelease(colorspace); | |
UIGraphicsEndImageContext(); | |
return [UIColor colorWithPatternImage:image]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment