Adding a background image to UILabel

How to add background image to UILabel in iPhone application. I tried to do this through IB, but with no result.

+50
ios
Jun 14 '10 at 14:10
source share
5 answers

Try this with the code:

Objective-C:

theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blah"]]; 

Swift:

 theLabel.backgroundColor = UIColor(patternImage: UIImage(named: "blah")!) 

Or put the UIImageView by the label (not recommended).




Update: placing the UIImageView behind the label is not recommended, because then you have to control two views. But if you have to do something that only UIImageView can do, this is a good approach. I suspect your application will work this way.

+145
Jun 14 '10 at 14:13
source share

if you want the image to be stretched to fill the width of the label. try this code.

 UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]; UIImage *img = [UIImage imageNamed:@"a.png"]; CGSize imgSize = myLabel.frame.size; UIGraphicsBeginImageContext( imgSize ); [img drawInRect:CGRectMake(0,0,imgSize.width,imgSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); myLabel.backgroundColor = [UIColor colorWithPatternImage:newImage]; 
+18
Sep 15 '13 at 2:59
source share

Swift 2.2: set background image with background color

  UIGraphicsBeginImageContextWithOptions(stationNameLabel.frame.size, false, 0.0) let context = UIGraphicsGetCurrentContext(); let components = CGColorGetComponents(color.CGColor) CGContextSetRGBFillColor(context, components[0], components[1], components[2], 0.4); CGContextFillRect(context, CGRectMake(0.0, 0.0, stationNameLabel.frame.size.width, stationNameLabel.frame.size.height)); targetImage.drawInRect(CGRectMake(0.0, 0.0, stationNameLabel.frame.size.width, stationNameLabel.frame.size.height)) let resultImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() label.backgroundColor = UIColor(patternImage: resultImage) 
0
Apr 12 '16 at 14:29
source share
  let messageBackgroundView = UIImageView() messageBackgroundView.image = UIImage(named: "chat-background") if messageBackgroundView.superview != lblChatdata.superview { lblChatdata.superview!.insertSubview(messageBackgroundView, belowSubview: lblChatdata) messageBackgroundView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ messageBackgroundView.leadingAnchor.constraint(equalTo: lblChatdata.leadingAnchor), messageBackgroundView.trailingAnchor.constraint(equalTo: lblChatdata.trailingAnchor), messageBackgroundView.topAnchor.constraint(equalTo: lblChatdata.topAnchor), messageBackgroundView.bottomAnchor.constraint(equalTo: lblChatdata.bottomAnchor), ]) messageBackgroundView.contentMode = .scaleAspectFill } 
0
Apr 27 '19 at 12:49 on
source share

@pixelfreak, your right. A UIColor instance with a color image template has serious performance issues when assigning it backroundcolor

 cell.cellLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!) 
-one
May 24 '16 at 20:58
source share



All Articles