如何在iOS中屏蔽UILabel-Objective-C [英] How to mask a UILabel in iOS - Objective-C
问题描述
我想对一个UIView进行子类化,并将四个UILabel放在另一个之上;最上面的标签是遮罩,第二个标签是带有文本的普通标签,第三个标签是带有纯背景且没有文本的标签.底部标签将与顶部第二个标签相同,但使用不同的颜色字体.当我发送第三个标签的宽度时,它将覆盖底部的标签,显示文本的局部视图.我想让第二个文本是一种颜色,而未覆盖的底部标签显示另一种颜色的字体.
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.
这是可能的吗?如果有人可以解释如何在Objective-C中进行掩盖,那也会有所帮助.
Is this possibe? If someone can explain how to mask in objective-C that will help too.
我试图构建一个类似于进度条的UIView,当进度条填充到60%时,我希望顶部文本以白色字体显示,而底部文本以其他颜色显示.
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.
推荐答案
您可以使用两个UILabel(一个在底部,一个嵌入在另一个视图中)来实现.
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];
然后,当您要更改进度时,可以设置 topContainer
的宽度.这应该剪辑 topLabel
.
Then, when you want to change the progress, you'd set the width of topContainer
. This should clip topLabel
.
这篇关于如何在iOS中屏蔽UILabel-Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!