user3661308
user3661308

Reputation:

Set Background Image to UILabel Properly in iOS

So How you will set a background image to UILabel.

Here is the ways i have tried after googling and reading some of Stackoverflow posts.

I have a image and want to fit / shrink according to size of UILabel.

1 --

lblDelivery.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Tick.png"]];

and result is :

enter image description here

2 --

UIImage *img = [UIImage imageNamed:@"Tick.png"];
CGSize imgSize = lblDelivery.frame.size;
UIGraphicsBeginImageContext( imgSize );
[img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lblDelivery.backgroundColor = [UIColor colorWithPatternImage:newImage];

and result is :

enter image description here

Please help on this issue. how to i can get this image in BG perfectly

Upvotes: 1

Views: 3843

Answers (1)

M Zubair Shamshad
M Zubair Shamshad

Reputation: 2741

You may use a custom button rather than label having no action against button.

For that you can use this piece of code to set the background image, and it will be stratched automatically.

[yourButton setBackgroundImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];

Although There is no action against this button, you may also disable user intraction on this button as

yourButton.userInteractionEnabled = NO;

Upvotes: 4

Related Questions