Francesco
Francesco

Reputation: 1047

Setting the background of a UILabel (transparency problem with .png files)

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.

Upvotes: 3

Views: 5671

Answers (6)

kjoelbro
kjoelbro

Reputation: 6316

I had the same problem, and the solution is:

[yourLabel setBackgroundColor:[UIColor clearColor]];

Changing opaque or alpha value didn't work.

Upvotes: 2

felixwcf
felixwcf

Reputation: 2107

Maybe you can try this one:

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

Upvotes: 1

akashivskyy
akashivskyy

Reputation: 45190

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];

Upvotes: 0

zhuchao
zhuchao

Reputation: 11

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

it must be work,goodLuck!

Upvotes: 0

amergin
amergin

Reputation: 3176

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

Upvotes: 0

glorifiedHacker
glorifiedHacker

Reputation: 6420

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.

Upvotes: 4

Related Questions