iOS:使图层不透明而不褪色文本
我的 UIView 上有一个带有图层的框架。我让图层设置背景颜色并将 alpha 设置为 0.5 并设置frame.backgroundColor =clearColor,以便人们可以看到其后面的线条。然而,它使得包含文本的子视图也淡出。我该如何防止这种情况?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setAlpha:kAlpha];
CALayer *layer = [self layer];
[layer setMasksToBounds:YES];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setCornerRadius:kCornerRadius];
}
return self;
}
- (id)init
{
if (self = [super init]) {
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
tileTitle = [[UILabel alloc] init];
tileTitle.textColor = [UIColor blackColor];
tileTitle.backgroundColor = [UIColor clearColor];
tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
tileDescription = [[UILabel alloc] init];
tileDescription.textColor = [UIColor blackColor];
tileDescription.backgroundColor = [UIColor clearColor];
tileDescription.font = [UIFont systemFontOfSize:11.0f];
tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:tileTitle];
[self addSubview:tileDescription];
}
return self;
}
I have a frame on my UIView with a layer. I have the layer set the background color and make the alpha 0.5 and make the frame.backgroundColor = clearColor so that people can see the lines behind it. However it makes the subviews which hold text also faded out. How do I prevent this?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setAlpha:kAlpha];
CALayer *layer = [self layer];
[layer setMasksToBounds:YES];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setCornerRadius:kCornerRadius];
}
return self;
}
- (id)init
{
if (self = [super init]) {
self.clipsToBounds = YES;
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
tileTitle = [[UILabel alloc] init];
tileTitle.textColor = [UIColor blackColor];
tileTitle.backgroundColor = [UIColor clearColor];
tileTitle.font = [UIFont boldSystemFontOfSize:13.0f];
tileDescription = [[UILabel alloc] init];
tileDescription.textColor = [UIColor blackColor];
tileDescription.backgroundColor = [UIColor clearColor];
tileDescription.font = [UIFont systemFontOfSize:11.0f];
tileDescription.lineBreakMode = UILineBreakModeTailTruncation;
[self addSubview:tileTitle];
[self addSubview:tileDescription];
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要更改视图而不是其子视图的透明度,您可以更改其背景颜色:
此方法也适用于 CALayer。
To change the transparency of the view, but not its subviews, you can change its background color:
This method also works with a CALayer.
您不能在所有 uiview 上设置 alpha 值,您必须使用 alfa 设置颜色。
you must not set alpha value on all uiview, you must se the color with alfa.
使用子视图(与 UIView 大小相同)并将子视图上的颜色和 alpha 设置为所需的透明度,而不是设置视图的 alpha。将顶层UIView的backgroundColor设置为clearColor。
Use a subview (with the same size as your UIView) and set the color and alpha on the subview to your desired tranparency, rather than setting the alpha of your view. Set the backgroundColor of the top-level UIView to clearColor.