How to set a background image to label - ios

I want to display a number on image like badge of size 30*28, which is placed upon button image,
I have an badge image to set up on the top of button image..
on top of badge image I should able to display text or number and my badge size is 30*28.
so, for this to achieve I want to set a label on top of my button image and so I want to set label background to some image called badge image.

found this in stack overflow itself as the above code just gives the background without the label
was useful for me
so sharing
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blah"]];
See: Adding Background image to UILabel

You can't add a background image to a UILabel, but you can add a UIImageView as a subview to a UILabel. Make sure the UILabel has backgroundColor = [UIColor clearColor]; for transparency.
E.g.
UIImageView *labelBackground = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"mybg.png"]];
[myLabel addSubview:labelBackground];
[labelBackground release];
myLabel.backgroundColor = [UIColor clearColor];
Should work. Untested.

To make the image fit in your UIview, try this code
UIImage *backgroundImage = [UIImage imageNamed:#"imageNameHere"];
UIGraphicsBeginImageContext(self.labelName.frame.size);
[backgroundImage drawInRect:CGRectMake(0, 0, self.labelName.frame.size.width, self.labelName.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.labelName.backgroundColor = [UIColor colorWithPatternImage:newImage];

Related

Background Image overlaps Refresh Control

In xcode 7.1, I have used the refresh control feature of a TableView.
I have also used the following code to set an image to the background
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[tempImageView setFrame:self.tableView.frame];
self.tableView.backgroundView = tempImageView;
This image overlaps the activity indicator on the app so you can't see if its working. Is there a way to put the activity indicator back on top?
Thanks
Use this line
self.tableView.backgroundColor =
[UIColor colorWithPatternImage:[UIImage imageNamed:#"image.png"]];
Instead of using
self.tableView.backgroundView = tempImageView;
Note: but your UIImage size should be greater/equal to UITableView size

Best way to dynamically change the width of an image view to match width of label text?

I'm constructing a UITableViewCell via a nib file. I also have a corresponding UITableViewCell subclass which the nib's outlets are connected to.
I need a dynamically sized UIImageView (only the width) to be the background of my tableviewcell's label. Is there a way this can be achieved via autolayout?
I've tried setting the imageview as a subview of the label in code:
[theLabel sizeToFit]
CGRect boundingRect = [theLabel.text boundingRectWithSize: theLabel.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:0 context:0];
UIImageView *background = [[UIImageView alloc]initWithFrame:boundingRect];
background.image = [UIImage imageNamed: #"validImageName"]];
[theLabel addSubview: background];
However this does not result in the correct frame. I've also considered setting the background of the label with [UIColor colorWithPatternImage: #"aValidImageName"]; but this does not include padding on the background image. Also, the background image contains rounded corners which I want to retain (which this method cuts). Any ideas are appreciated.
Try this,
1) You can use Autoresizing to change image width according to size of screen.
(OR)
2) Set Custom frame for your imageview
Ex:
UIImageView *background = [[UIImageView alloc]initWithFrame:(0,0,Your_Custom_Width_Size_Value,100)];
background.image = [UIImage imageNamed: #"validImageName"]];
[theLabel addSubview: background];
(or)
//Change width according to the label
UIImageView *background = [[UIImageView alloc]initWithFrame:(0,0,Your_Label_Name.frame.size.width,100)];
background.image = [UIImage imageNamed: #"validImageName"]];
[theLabel addSubview: background];
I would advise you to use a UIView object as a container and add your UIImageView and UILabel as subViews to it. Add constraints to the Image View and the Label so that their height and width are always equal to the superview. Then just give the container UIView whatever frame you want.

Overlay textLabel and UIImageView in UITableViewCell

I have a problem positioning a textLabel and detailTextLabel into a custom UITableViewCell that has a UIImageView appearing behind the textLabel and detailTextLabel.
Refer to the image - it will make more sense
--> what you're looking a UITableView, where each bubble represents a different UITableViewCell.
Essentially, I am trying to fit the textLabel and detailTextLabel into the bubble you see by expanding the bubble dimensions. However, no matter what I try the bubble will not change it's width and height even if I change the UIImageView frame or bounds or contentMode.
Here is the relevant code in cellForRowAtIndexPath:
//Set font and style
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
// Assign our own background image for the cell
UIImage *cellBackgroundImage = [UIImage imageNamed:#"Chat.png"];
UIImageView *cellBackgroundImageView = [[UIImageView alloc] initWithImage:cellBackgroundImage];
//[cellBackgroundImageView setBounds:CGRectMake(0, 0, 20, 20)];
//[cellBackgroundImageView setImage:cellBackgroundImage];
//cellBackgroundImageView.frame = CGRectMake(cellBackgroundImageView.frame.origin.x, cellBackgroundImageView.frame.origin.y, 20, 20);
//cellBackgroundImageView.bounds = CGRectMake(cellBackgroundImageView.frame.origin.x, cellBackgroundImageView.frame.origin.y, 0, 0);
cellBackgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
cellBackgroundImageView.image = cellBackgroundImage;
cell.backgroundView = cellBackgroundImageView;
Any help would be appreciated.
Don't do it here, I did it by writing frame of image and label in LayoutSubviews method in custom cell class.
Just
-(void)layoutSubviews
{ setframe: for both UI components }
Dont use Aspect FIll, use either scaleToFill, or set image to image view using imageCapInsets .
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
For your Image, it must be like
UIImage *cellBackgroundImage = [[UIImage imageNamed:#"Chat.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 70, 40, 20) resizingMode:UIImageResizingModeStretch]

UITableViewCell imageView overlaps text in cell

I have a cell that has some text in it and was trying to add an image to the cell.
This is what Ive tried:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
imageView.backgroundColor = [UIColor clearColor];
[imageView.layer setCornerRadius:5.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:[UIImage imageNamed:#"test2.jpeg"]];
[cell.contentView addSubview:imageView];
cell.imageView.clipsToBounds = YES;
This is what it looks like right now:
Is there a way to avoid the image from overlapping the text in the cell? Thanks in advance!
tableviewcells have their own imageView that will appear to the left of the content or atleast to the left of the cells text.
You are creating an extra imageView that just happens to have the same name.
Just change the location
cell.imageView.center = CGPointMake(x,y);
Check out https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW11 which has a nice example of how to use the built-in UITableViewCell image view.
Every UITableViewCell already has an UIImageView property 'imageView', but it's only shown if you set the imageView properties image property (cell.imageView.image = /* image code */;)
The best thing you can do is create a custom UITableViewCell. Or perhaps add another UILabel to the current cell and set your text in that instead of using the default UILabel.

setting an image in background of a label programmatically

I have a label (many labels in fact) now I want an image in the background of my label (same image) so that text writtenin my label is shown to user. How to do it programmatically?
The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.
CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:#"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...
[self.view addSubview:theImageView];
[self.view addSubview:theLabel];
[theImageView release];
[theLabel release];

Resources