Reputation: 43
I draw this with UILabel
subclass using with overriding drawRect
method but I want to change color and want to set alpha value with slider.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
// let the superclass draw the label normally
[super drawRect:rect];
CGContextConcatCTM(context, CGAffineTransformMake(1, 0, 0, -1, 0, CGRectGetHeight(rect)));
// create a mask from the normally rendered text
CGImageRef image = CGBitmapContextCreateImage(context);
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), CGImageGetBitsPerPixel(image), CGImageGetBytesPerRow(image), CGImageGetDataProvider(image), CGImageGetDecode(image), CGImageGetShouldInterpolate(image));
CFRelease(image); image = NULL;
// wipe the slate clean
CGContextClearRect(context, rect);
CGContextSaveGState(context);
CGContextClipToMask(context, rect, mask);
if (self.layer.cornerRadius != 0.0f) {
CGContextAddPath(context, CGPathCreateWithRoundedRect(rect, self.layer.cornerRadius, self.layer.cornerRadius, nil));
CGContextClip(context);
}
CFRelease(mask); mask = NULL;
[maskedBackgroundColor set];
CGContextFillRect(context, rect);
CGContextRestoreGState(context);
}
Upvotes: 1
Views: 959
Reputation: 4735
Create a few properties:
/** The value for the red channel of the colour of the label. */
@property (nonatomic, strong) CGFloat rValue;
/** The value for the green channel of the colour of the label. */
@property (nonatomic, strong) CGFloat gValue;
/** The value for the blue channel of the colour of the label. */
@property (nonatomic, strong) CGFloat bValue;
/** The value for the alpha channel of the colour of the label. */
@property (nonatomic, strong) CGFloat aValue;
Update them with their respective UISlider
values.
In each UISlider
callback call - (void)setNeedsDisplay
to trigger the drawRect
.
In the drawRect
do this:
UIColor fillColour = [[UIColor alloc] initWithRed:self.rValue green:self.gValue blue:self.bValue alpha:self.aValue];
[fillColour setFill];
This will set the fill colour to the colour determined by the sliders. Then when you fill your rect, it will be that colour.
Upvotes: 1