adding image in UILabel in iPhone - 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.

Related

Adding an Image programmatically between an UIImage and UILabel

I have an UIImage above which I am supposed to add another UIImage programmatically, above which there will be an UILabel. I successfully added the Image but it overlaps the Label.
If 1. BG image 2.Image to be added programmatically 3. Label,
then the sequence I want is,
1
2
3
and what I get is
1
3
2
2 overlaps my Label. I have tried Send to Back, Bring to Front and bringSubviewToFront. It did not work.
UIImageView *img2=[[UIImageView alloc] initWithImage: [UIImage imageNamed: #"TDK 3 Speaker.png"]];
//img2.backgroundColor=[UIColor clearColor];
[BgImage addSubview:img2];
UILabel *lblText=[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 200, 40)];
lblText.text=#"I am Here";
//lblText.backgroundColor=[UIColor clearColor];
[BgImage addSubview:lblText];
What you should do is add the particular images in that sequence itself, i.e. in the ViewWillAppear or the ViewDidLoad method use the method
self.view addSubview:BGimage .
Then add the next image on the previous one like
BGimage addSubview:image2 .
Similarly add the UILabel on the Image 2 like
Image2 addSubview:Label
This will put your images and the label in the particular sequence you want :)
try this, add QuartzCore.framework and in your header file
#import <QuartzCore/QuartzCore.h>
and in you implementation file
your_Label.layer.zPosition=your_secondImage.layer.zPosition+1;
The order of adding is important here the later one comes in front
Add subview should be called in an order first BGImage then Image and then Label
If you want to show BG Image (1) then you can also use UIView instead of UIImageView You can also do the samething with UIImageView as well.
[yourUIView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourBackGroundImage"]]];
[yourUIView setFrame:CGRect(x,y,width,height)]; //yourExpectedFrame;
Now, create a UIImageView (2) with UIImage and also set a frame within that UIView, also create a UILabel (3) and give it frame as per the UIView and accordingly with UIImageView. That's all. You done!.
i m not sure,but you try this:
Set the property masksToBounds: YES for the UIImageView.

iPhone - how to remove black space beneath the navigation bar after reducing the height

I had to adjust the height of the UINavigationBar for this iPhone app.
Now when I use the smaller navBar in a tableview, there is a black space beneath it.
I would like to see it gone...
Can anyone help me? Much appreciated!
visual: http://tinypic.com/r/2gwchg2/6
Set your navigation bar frame and make it above reduce y direction in frame.Try this.
self.navigationController.navigationBar.frame=CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);
Make it y=-10 and height=30; and check and adjust according your requirement.
UIView *BtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0,300,30)];
UILabel *compete_Label=[[UILabel alloc]initWithFrame:CGRectMake(100, 5, 60, 20)];
[compete_Label setText:#"Your Title name"];
compete_Label.backgroundColor=[UIColor clearColor];
[compete_Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
compete_Label.textColor=[UIColor whiteColor];
[BtnView addSubview:compete_Label];
[self.View addSubView:BtnView];
Try this code I think that will be work for you.
If you make the navigation bar smaller, then you should make the tableview frame position accordingly such that its origin-y to be set to bottom of the navigation bar.
Set the tableview origin-y and adjust the height of the tableview.

iPhone UITextField and UIButton in one grey frame

I would like to create something like this:
I would like to place an UITextField with an UIButton into one grey frame, and I would like the button's border to be contained by this grey frame.
Please somebody show me some sample code for this!
Thx!
1. Use a UIImageView of required rect. Set the background image to the gradient gray colour image.
UIImageView*myImageView=[[UIImageView alloc] initWithFrame:yourFrame];
[myImageView setImage:[UIImage imageNamed:#"grayBackground.png"];
[self.view addSubview:myImageView];
2. Use a round rect. UITextField make it a subview of the image view. Use a place holder as "Write a reply..." make it a subview to your imageview.
UITextField*myField=[[UITextField alloc] initWithFrame:yourRect];
[myField setBorderStyle:UITextBorderStyleRoundedRect];
[myField setPlaceholder:#"Write a reply..."];
[myImageView addSubview:myField];
3. Use a UIButton with type Custom and the send image as its background image, make it a subview to your imageview.
UIButton*myButton=[UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:yourRect]
[myButton setBackgroundImage:[UIImage imageNamed:#"sendImage.png"] forState:UIControlStateNormal];
[myImageView addSubView:myButton];
Simple way is to create title bar with grey background and delete the title.and place textfield and button which u want to place it.configure the size and button outside the title bar and drag it to the title bar..u'll get like the image which u have got.

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

How to set the background of a UITableView to an image?

I am using a UITableViewController which uploads a table. I have a Nib File with UITableView in it.Now I want to set the background of the tableView either from interface builder or from the TableViewController to an image.
How to do that.
OK so I created an UIImage in my controller. Now when I add where do I need to add it.
When I try adding it and set the color of tableView to clearColor, it just shows me the Image and not the table although I make sure that image is being sent to back of all views.
Guys Please note that I am not dealing a UIView and adding a tableView as its subview But I am dealing with a UITableView .
Place a UIImageView behind the UITableView, then do this:
tableView.backgroundColor = [UIColor clearColor];
Somehow playing around I was able to find out a way.
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"parentViewBackground.png"]];
the image size should be
2x - 640*960
1x - 320*480
the size smaller than above, it will be tiled.
The backgroundColor = [UIColor colorWithPatternImage: image] method rkbang outlines works great. Here is another method to achieve the same effect by adding a new view to the parentViewController. This would be useful if you want to mask other contents the parentViewController might have.
In your UITableViewController subclass, in -viewDidLoad insert:
//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:#"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];
[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];
You may wish to keep track of the backgroundImageView so you can remove it or replace it. The code example above leaves it to the parentViewController to manage the new view. If you're loading and unloading this UITableView, you'll be leaking these UIImageViews with every load unless the parentViewController is releasing them at the same rate somehow.
What you can do is set the backgroundColor property of the tableview to [UIColor clearColor] and have a UIImageView of the same size and position underneath your TableView to show your background.
TableView properties include background color. Set this to clearColor; and put the image behind it. (In IB I think you'll have to delete the tableview add an image and then add your tableview back again)
if you're subclassing UITableViewController you get a tableview for free, no ivar or nib required, however since you're using a background I would stick with IB.
You can use the table-view's backgroundView. This worked for me.
[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"????.png"]]];
where ????.png is your background image.