如何在 iOS 中屏蔽 UILabel - Objective-C

发布于 2024-10-17 23:15:22 字数 309 浏览 3 评论 0原文

我想对 UIView 进行子类化,并将四个 UILabels 放在另一个之上;顶部标签将是掩模,第二个标签将是带有文本的普通标签,第三个标签是具有纯色背景且没有文本的标签。底部标签将与顶部第二个标签相同,但字体颜色不同。当我发送第三个标签的宽度时,它将覆盖底部标签,显示文本的部分视图。我想让第二个文本为一种颜色,而未覆盖的底部标签显示另一种颜色字体。

这可能吗?如果有人可以解释如何在 Objective-C 中进行屏蔽,那也会有帮助。

我试图构建一个像进度条一样的 UIView,当进度条填充到 60% 时,我希望顶部文本以白色字体颜色显示,而底部文本以不同颜色显示。

I want to sub-class a UIView and place four UILabels over top one another ; top label will be the MASK, 2nd label will be a normal label with text, the 3rd label is a label with solid background with no text. the bottom label will the same as the top 2nd label with a different color font. when i sent the width of the third label it will cover up the bottom label showing a partial view of the text. I want to have the 2nd text be one color while the uncoverd bottom label display another color font.

Is this possibe? If someone can explain how to mask in objective-C that will help too.

I trying to build a UIView that acts like a progress bar, as the bar fill to 60%, I want to top text to show in white font color, when the bottom text shows in a different color.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-10-24 23:15:22

您可以使用两个 UILabel 来完成此操作,一个位于底部,一个嵌入在顶部的另一个视图中。

UILabel *bottomLabel = ...;
[self.view addSubview:bottomLabel];

UIView *topContainer = [[UIView alloc] initWithFrame:bottomLabel.frame];
topContainer.clipsToBounds = YES;
topContainer.opaque = NO;
topContainer.backgroundColor = [UIColor clearColor];

UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bottomLabel.frame.size.width, bottomLabel.frame.size.height)];
topLabel.text = bottomLabel.text;
topLabel.opaque = NO;
topLabel.backgroundColor = [UIColor clearColor];

[topContainer addSubview:topLabel];
[self.view addSubview:topContainer];

然后,当您想要更改进度时,您可以设置 topContainer 的宽度。这应该剪辑 topLabel

You could do it with two UILabels, one on the bottom, and one embedded in another view on top.

UILabel *bottomLabel = ...;
[self.view addSubview:bottomLabel];

UIView *topContainer = [[UIView alloc] initWithFrame:bottomLabel.frame];
topContainer.clipsToBounds = YES;
topContainer.opaque = NO;
topContainer.backgroundColor = [UIColor clearColor];

UILabel *topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, bottomLabel.frame.size.width, bottomLabel.frame.size.height)];
topLabel.text = bottomLabel.text;
topLabel.opaque = NO;
topLabel.backgroundColor = [UIColor clearColor];

[topContainer addSubview:topLabel];
[self.view addSubview:topContainer];

Then, when you want to change the progress, you'd set the width of topContainer. This should clip topLabel.

卷耳 2024-10-24 23:15:22

与其使用四个 UILabel,为什么不子类化 UILabel 并在 drawRect: 方法中自己绘制它呢?它看起来像:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the mask
    CGContextClipToMask(context, self.bounds, /* mask image */);

    // Draw the text in a different font
    [self.text drawInRect:rect withFont:/* alternate font */];

    // Draw a solid background
    CGContextSetRGBFillColor(context, ...);
    CGContextFillRect(context, rect);

    // Draw the text normally
    [super drawRect:rect];
}

为了方便起见,您可以为子类创建遮罩图像和替代字体属性。

Rather than using four UILabels, why not subclass UILabel and draw it yourself in the drawRect: method? It would look something like:

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the mask
    CGContextClipToMask(context, self.bounds, /* mask image */);

    // Draw the text in a different font
    [self.text drawInRect:rect withFont:/* alternate font */];

    // Draw a solid background
    CGContextSetRGBFillColor(context, ...);
    CGContextFillRect(context, rect);

    // Draw the text normally
    [super drawRect:rect];
}

You could make the masking image and the alternate font properties of your subclass, for convenience.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文