首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用颜色填充UILabel的透明文本

用颜色填充UILabel的透明文本
EN

Stack Overflow用户
提问于 2014-04-05 09:14:14
回答 1查看 923关注 0票数 1

我用UILabel子类来绘制它,使用重写的drawRect方法,但是我想要改变颜色,并且希望用滑块设置alpha值。

代码语言:javascript
复制
- (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);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-05 13:14:02

创建了几个属性:

代码语言:javascript
复制
/** 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 中的执行以下操作:

代码语言:javascript
复制
UIColor fillColour = [[UIColor alloc] initWithRed:self.rValue green:self.gValue blue:self.bValue alpha:self.aValue];
[fillColour setFill];

这将将填充颜色设置为由滑块决定的颜色。然后,当你填充你的直角,它将是那种颜色。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22878891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档