Adding Background image to UILabel

Ios

Ios Problem Overview


How can I add a background image to a UILabel in an iPhone Application. I've tried to do it through IB but with no result.

Ios Solutions


Solution 1 - Ios

Try doing it with code:

Objective-C:

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

Swift:

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

Or place an UIImageView behind the label (not recommended).


Update: placing an UIImageView behind a label was not recommended because then you would have to manage two views. But if you must do something that only an UIImageView can do this is still a good approach. I suspect that your app will actually run better this way.

Solution 2 - Ios

if you want that image should be stretched to fill the Label width. 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];

Solution 3 - Ios

Swift 2.2: set the 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)

Solution 4 - Ios

 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
        }

Solution 5 - Ios

@pixelfreak, your right. UIColor instance with color image pattern has serious performance issues when assigning it to backroundcolor

cell.cellLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoyView Question on Stackoverflow
Solution 1 - IosEvadne WuView Answer on Stackoverflow
Solution 2 - IosLê TrinhView Answer on Stackoverflow
Solution 3 - IosBill ChanView Answer on Stackoverflow
Solution 4 - IosDavender VermaView Answer on Stackoverflow
Solution 5 - IosIkechukwu Henry OdohView Answer on Stackoverflow