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

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.

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

Related

How to add custom image to a UITableviewController in Xcode?

I am new to Xcode, and I am currently building my first app. I've searched long and hard to try and find the proper tutorial but I can't find one. I am looking for a way to be able to insert a custom background in my UITableViewController. I can change the color, but that is it. What I am looking to do is set my PNG image behind the static cells I've created, and drop the opacity on those cells so the custom image comes through. Can someone please give me a way to do this either through the IB (storyboard) or through the coding. Much appreciated!! Thanks in advance!
I think the easier and correct way is to:
[tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]]];
You can include this code in viewDidLoad to make it work.
for cell's background :, do this in willDisplayCell:atIndexPath: method
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImageimageNamed:#"image.png"]]];
CGRect Frame = cell.backgroundView.bounds;
cellImageView.frame = newFrame;
[cell.backgroundView addSubview:cellImageView];
for tableview background , do this in viewDidLoad: method
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BackgroundPattern.png"]];
UIImageView *ivwCellBackground=[[UIImageView alloc]init];
[ivwCellBackground setImage:[UIImage imageNamed:CELL_BACKGROUNGIMAGE]];
cell.backgroundColor=[UIColor clearColor];
[cell setBackgroundView:ivwCellBackground];
[ivwCellBackground release];
ivwCellBackground=nil;

Background transparancy of a textfield

I have an application that contains a textfield.
Is there a way to change background transparency of this textfield in code?
Yes you can do it.
You can set transparency by setting its alpha. You can pic color code with digital color meter ( its a inbuilt application in mac).
if you set alpha 1 then there will be no transparency. set transparency according to your need (set alpha less then 1).
Try this:
textfield.backgroundColor=[[UIColor colorWithRed:208.0/255.0 green:15.0/255.0 blue:202.0/255.0 alpha:0.6] CGColor];
You can set with below mentioned code in Xamarin
txtNumber.BackgroundColor = UIColor.White;
txtNumber.Alpha = 0.5f;
Reference 1: transparent UITextView
Reference 2: How do I create a transparent UITextField?
Similary:
txtNumber.BackgroundColor = UIColor.Clear;
use Custom UITextField
UITextField *yourTextField= [[UITextField alloc] initWithFrame:frame];
yourTextField.borderStyle = UITextBorderStyleNone;
[yourTextField setBackgroundColor:[UIColor clearColor]];
Just put bellow code for white background
[yourTextField setBackgroundColor:[UIColor whiteColor]];
or if want to clear Color then bellow code
[yourTextField setBackgroundColor:[UIColor clearColor]];
so simple
:)

adding image in UILabel in iPhone

i have UIlabel with image.my text are won't come into top of the image i want to show my text in top of the image.
image1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"salarytax.png"]];
this is my code to adding image to label. this image is a output of that code. how can i show the text in top of the image
try adding the UILabel on the image
[myimageview addSubView: myLabel];
[myImageView bringSubviewToFront: myLabel];
If you have just taken the UILabel and trying to set its background as an image then dont think it would create a problem.
If you are able to view the text on the label but you want the text to be vertically on top then you can set its contentVerticalAlignment property to top.
Hope this helps..
UILabel *imageLabel;
[imageLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"logo.png"]]];
[imageLabel setText:#"Khalid Usman"];
[imageLabel setTextColor:[UIColor blackColor]];
Note: I have checked it for conformation then i post. It's working.

colorWithPatternImage and setCornerRadius issue

Hello I'd like to achieve at the same time rounded corners and a background composed by tiling a little png (OPERATOR_VIEW_BACKGROUND_IMAGE). My main goal is to allow a designer to fill the background of a View by inserting the right image in the project resources.
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
[triggerView.layer setCornerRadius:borderRadius];
[triggerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]]];;
I don't know why but triggerView loose the CornerRadius setting when I add the last line.
triggerView is a UIView built with interface builder and modified in its superView viewDidLoad programmatically, with the code above.
Where I'm wrong?
EDIT: I haven't mentioned that If I use a simple UIColor like: [UIColor orangeColor] It works well. So It's something related to the patternImage thing.
EDIT: I've tried also this code, working on the layer background of my view:
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
triggerView.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE];
triggerView.layer.backgroundColor = [UIColor colorWithPatternImage:img].CGColor;
triggerView.layer.cornerRadius = radius;
[img release];
[self.view addSubview:triggerView];
Now I get a transparent background but the corners are rounded;
OK, thanks to Ben's comment and this Blog entry I've found this solution:
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
triggerView.layer.masksToBounds = YES;
triggerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];
triggerView.layer.cornerRadius = radius;
[self.view addSubview:triggerView];
Seems that triggerView.layer.masksToBounds = YES; was the missing piece, but I still don't understand why triggerView.layer.cornerRadius = radius; alone didn't suffice.
Try setting the content property of the layer with the CGImageRef of the image:
triggerView.layer.contents = (id) [[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE] CGImage];
You may have to alloc or retain the UIImage to prevent it being autoreleased...
With iOS SDK 4.0 and 4.1, colorWithPatternImage method has got a bug that show badly an image...
I used this method and I had this bug but I could resolve using another method...
Try to use the initWithPatternImage method of UIColor class:
UIColor *imageBg = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];
This worked greatly for me...
Unfortunately I never used cornerRadius method and I don't know a possibly solution for this other problem.

iPhone - UIView's backgroundColor - using a png with transparency?

I'm setting the background color of my UIView (which is within a UIViewController) as follows:
myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myTransparentBG.png"]];
I've even tried messing with [myView setOpaque:NO]; but the image doesn't appear transparent. It has a black background to it. Am I abel to fix this programmatically? Otherwise, how are we supposed to set a transparent background to our view?
This seems like a question that should have been asked before, but I couldn't find an answer.
When using a transparent pattern, after setting the backgroundColor property, you need to set opaque property to NO on both view and its layer. Here's a sample:
UIView* paperView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
paperView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"paper.png"]];
[paperView.layer setOpaque:NO];
paperView.opaque = NO;
I haven't ever actually done this, but here's what I'd try.
Set the UIView's .backgroundColor to [UIColor clearColor]. Then put a UIImageView containing your PNG as the "lowest" item on the UIView's stack of subviews. Make sure the UIImageView's background color is also clear.
Use this: myView.backgroundColor = [UIColor clearColor];
This will set it to a transparent background.