gpt4 book ai didi

ios - 用颜色填充 UILabel 的透明文本

转载 作者:行者123 更新时间:2023-11-29 12:49:00 25 4
gpt4 key购买 nike

can we change UILabel text colour with different text

我用 UILabel 子类绘制它并使用重写的 drawRect 方法,但我想更改颜色并使用 slider 设置 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);

}

最佳答案

创建一些属性:

/** 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];

这会将填充颜色设置为由 slider 确定的颜色。然后当你填充你的矩形时,它将是那种颜色。

关于ios - 用颜色填充 UILabel 的透明文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22878891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com