开发者

Adding Background image to UILabel

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.开发者_如何学C


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.


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];


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)


 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
        }


@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")!)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜