设置 UILabel 的背景(.png 文件的透明度问题)

发布于 2024-11-29 14:11:45 字数 267 浏览 2 评论 0原文

我试图通过设置 UILabel 的属性 backgroundColor 来设置 UILabel 的背景。

但我有一个问题:backgroundColor 是从 .png 文件(具有透明度)创建的,结果是透明部分显示为黑色。

有没有不需要自定义子类的解决方案?

更新

我尝试将 UILabel 属性 opaque 设置为 NO 但问题仍然存在。

I'm trying to set the background of a UILabel by setting its property backgroundColor.

But I have a problem: the backgroundColor is created from a .png file (with transparency) and the result is that the transparent part appear in black.

Any solution without the need of custom subclasses?

Update

I tried setting the UILabel property opaque to NO but the problem is still there.

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

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

发布评论

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

评论(6

寄与心 2024-12-06 14:11:45

听起来您可能忘记更改 UILabel 的 opaque 属性:

label.opaque = NO;

或者,您可以在 Xcode 的 InterfaceBuilder 视图中设置此属性。

It sounds like you might have forgotten to change the opaque property of the UILabel:

label.opaque = NO;

Alternatively, you can set this property in the InterfaceBuilder view of Xcode.

似狗非友 2024-12-06 14:11:45

我遇到了同样的问题,解决方案是:

[yourLabel setBackgroundColor:[UIColor clearColor]];

更改不透明或 alpha 值不起作用。

I had the same problem, and the solution is:

[yourLabel setBackgroundColor:[UIColor clearColor]];

Changing opaque or alpha value didn't work.

烟织青萝梦 2024-12-06 14:11:45

也许你可以尝试这个:

[your_label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];

Maybe you can try this one:

[your_label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];
小梨窩很甜 2024-12-06 14:11:45

您还需要将其保存为 png24 或 24 位 png 文件 - 至少从 PhotoShop 保存时是这样

You'll also need to save it as a png24 or 24bit png file - at least that's the case when saving from PhotoShop

冷情 2024-12-06 14:11:45
[footer setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Favourite_商品文字背景.png"]]];
[footer setOpaque:NO];

一定是工作,祝你好运!

[footer setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Favourite_商品文字背景.png"]]];
[footer setOpaque:NO];

it must be work,goodLuck!

再见回来 2024-12-06 14:11:45

将其opaque 属性更改为NO


如果没有帮助,您可以在标签后面添加 imageView:

[yourLabel setOpaque:NO];
[yourLabel setBackgroundColor:[UIColor clearColor]];
UIImageView *labelBkg = [[UIImageView alloc] initWithFrame:yourLabel.frame];
[labelBkg setImage:[UIImage imageNamed:@"yourImageName.png"]];
[self.view insertSubview:labelBkg belowSubview:yourLabel];
[labelBkg release];

Change its opaque property to NO.


If it doesn't help, you can add imageView behing your label:

[yourLabel setOpaque:NO];
[yourLabel setBackgroundColor:[UIColor clearColor]];
UIImageView *labelBkg = [[UIImageView alloc] initWithFrame:yourLabel.frame];
[labelBkg setImage:[UIImage imageNamed:@"yourImageName.png"]];
[self.view insertSubview:labelBkg belowSubview:yourLabel];
[labelBkg release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文