
我用UILabel子类来绘制它,使用重写的drawRect方法,但是我想要改变颜色,并且希望用滑块设置alpha值。
- (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);
}发布于 2014-04-05 13:14:02
创建了几个属性:
/** 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;用各自的UISlider值更新它们。
在每个UISlider中,调用- (void)setNeedsDisplay来触发drawRect。
drawRect 中的执行以下操作:
UIColor fillColour = [[UIColor alloc] initWithRed:self.rValue green:self.gValue blue:self.bValue alpha:self.aValue];
[fillColour setFill];这将将填充颜色设置为由滑块决定的颜色。然后,当你填充你的直角,它将是那种颜色。
https://stackoverflow.com/questions/22878891
复制相似问题