I am using a UIPickerView that reads from an array that has a list of hex color codes, which are then used to fill each row of the picker view with all the different colors. I am using a method that converts the hex codes into RGB codes, and a UIlabel to change the color of each row. My for loop to change the UILabel color in each row of the UIPickerView works just fine, but when I try to perform the same color change using the same hex code conversion with the stroke color of a circle I'm drawing with CAShapeLayer in the view, nothing happens . It seems that UIColor isn't accepting the hex code conversion, although the UILabel's background for each row in the UIPickerView does. Anyone have an idea of why this isn't working?
Here's the for loop that changes the UILabel's background color for each row in the UIPickerView:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *pickerLabel = [[UILabel alloc]init];
for (int currentIndex=0; currentIndex<[self.colorArray count]; currentIndex++) {
[pickerLabel setBackgroundColor: [self colorWithHexString:[self.colorArray objectAtIndex:row]]];
}
return pickerLabel;
}
Here's the UIPickerView delegate method where I'm trying to change the stroke color of the CAShapeLayer I'm working with:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *rowColorHex = [self.colorArray objectAtIndex:[self.picker selectedRowInComponent:0]];
[self.circleLayer setStrokeColor: [self colorWithHexString: rowColorHex];
//NSLog(@"%@", rowColorHex);
}
Here is how I create the circle shape. Notice that I've omitted the initial stroke color. I can assign a stroke color in the same manner within the UIPickerView's delegate method where my issue is, and it works just fine:
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.lineWidth = 50;
circleLayer.fillColor = [[UIColor clearColor] CGColor];
//circleLayer.strokeColor = [[UIColor redColor] CGColor];
[self.imageToBeCropped.layer addSublayer:circleLayer];
self.circleLayer = circleLayer;
And finally, here is the hex conversion method:
-(UIColor*)colorWithHexString:(NSString*)hex{
NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6) return [UIColor grayColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
// Scan values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
Core Animation layers don’t accept UIKit types—you need to use the Core Graphics equivalent, in this case CGColorRef
. You’re doing that in the code where you create it already, but your -colorWithHexString:
method returns a UIColor
.
[self.circleLayer setStrokeColor:[[self colorWithHexString: rowColorHex] CGColor]];