iOS:使图层不透明而不褪色文本

发布于 2025-01-02 10:52:50 字数 1388 浏览 3 评论 0原文

我的 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;
}

Screenshot

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

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

发布评论

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

评论(3

萌辣 2025-01-09 10:52:50

要更改视图而不是其子视图的透明度,您可以更改其背景颜色:

myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];

此方法也适用于 CALayer。

To change the transparency of the view, but not its subviews, you can change its background color:

myView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.25];

This method also works with a CALayer.

筱果果 2025-01-09 10:52:50

您不能在所有 uiview 上设置 alpha 值,您必须使用 alfa 设置颜色。

    [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];

you must not set alpha value on all uiview, you must se the color with alfa.

    [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
小伙你站住 2025-01-09 10:52:50

使用子视图(与 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.

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