Created
October 24, 2012 21:36
-
-
Save vibrazy/3949066 to your computer and use it in GitHub Desktop.
Create a CAGradientLayer from a CSS string in the "background-image(linear-gradient(top, #cb60b3 0%,#c146a1 50%,#a80077 51%,#db36a4 100%));" format
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
+(CAGradientLayer *)layerFromArray:(NSArray *)gradientsArray | |
{ | |
//create gradient from gradient | |
NSMutableArray *arrayOfColors = [NSMutableArray arrayWithCapacity:10]; | |
NSMutableArray *arrayOfPositions = [NSMutableArray arrayWithCapacity:10]; | |
for (NSString *string in gradientsArray) | |
{ | |
NSArray *arr = [string componentsSeparatedByString:@","]; | |
//color as hex | |
NSString *hex = [arr objectAtIndex:0]; | |
//float value | |
float colorPosition = [[arr objectAtIndex:1] floatValue]; | |
[arrayOfColors addObject:(id)[UIColor colorWithHexString:[hex uppercaseString]].CGColor]; | |
[arrayOfPositions addObject:[NSNumber numberWithFloat:colorPosition]]; | |
} | |
CAGradientLayer *headerLayer = [CAGradientLayer layer]; | |
headerLayer.colors = [NSArray arrayWithArray:arrayOfColors]; | |
headerLayer.locations = [NSArray arrayWithArray:arrayOfPositions]; | |
return headerLayer; | |
} | |
+(CAGradientLayer *)css3Style:(NSString *)string | |
{ | |
//will find any pattern of #cb60b3 0%,#cb60b3 0% | |
//CSS > @include background-image(linear-gradient(top, #cb60b3 0%,#c146a1 50%,#a80077 51%,#db36a4 100%));; | |
CAGradientLayer *gradient = nil; | |
NSString *regexStr = @"#[a-zA-Z0-9]{3,6} [0-9]{1,3}%"; | |
NSError *error = nil; | |
NSRegularExpression *testRegex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error]; | |
if( testRegex == nil ) NSLog( @"Error making regex: %@", error ); | |
NSInteger matches = [testRegex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])]; | |
__block NSMutableArray *arrayOfGradients = [NSMutableArray arrayWithCapacity:10]; | |
if (matches>0) { | |
[testRegex enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { | |
//separate values into array | |
NSArray *values = [[string substringWithRange:result.range] componentsSeparatedByString:@" "]; | |
//convert percent to a float value | |
float percentValue = [[[values objectAtIndex:1] stringByReplacingOccurrencesOfString:@"%" withString:@""] floatValue]; | |
//find the value from 100 | |
float floatValue = (float)percentValue/100.0f; | |
//create new string by appending , in between | |
NSString *newString = [[NSArray arrayWithObjects:[values objectAtIndex:0],[NSString stringWithFormat:@"%f",floatValue], nil] componentsJoinedByString:@","]; | |
[arrayOfGradients addObject:newString]; | |
}]; | |
//return gradient | |
gradient = [self layerFromArray:arrayOfGradients]; | |
} | |
return gradient; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment