Reputation: 11780
To set the background of my UILabel, I use
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blah"]];
Now my problem is that the background image is actually bigger than the size of my UILabel. Is there a simple way to make the background shrink to the size of the UILabel?
Upvotes: 1
Views: 234
Reputation: 1020
Edit - misunderstood question.
No, label is unable to do that. You'll probably need to use a transparent label with an UIImageView underneath with same size as the label and contentMode
set to value you need.
Upvotes: 1
Reputation: 11780
Here is how I finally figured it out
- (void)reziseLabelBackgound
{
UIImage *rawImage = [UIImage imageNamed:@"blah"];
CGSize bkgSize = self.myLabel.bounds.size;
UIGraphicsBeginImageContext(bkgSize);
[rawImage drawInRect:CGRectMake(0,0,bkgSize.width,bkgSize.height)];
UIImage *bkgImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.myLabel.backgroundColor = [UIColor colorWithPatternImage:bkgImage];
}
Upvotes: 2