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 transpa开发者_开发问答rent 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.
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.
I had the same problem, and the solution is:
[yourLabel setBackgroundColor:[UIColor clearColor]];
Changing opaque or alpha value didn't work.
Maybe you can try this one:
[your_label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]]];
You'll also need to save it as a png24 or 24bit png file - at least that's the case when saving from PhotoShop
[footer setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Favourite_商品文字背景.png"]]];
[footer setOpaque:NO];
it must be work,goodLuck!
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];
精彩评论