learner
learner

Reputation: 11780

shrink uilabel background to fit allotted space

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

Answers (2)

Rychu
Rychu

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

learner
learner

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

Related Questions