为UILabel添加背景图片

53
我该如何在 iPhone 应用中为 UILabel 添加背景图片?我尝试通过 IB 进行操作,但没有成功。
5个回答

150

试着用代码实现:

Objective-C:

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

Swift:


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

或者在标签后面放置一个UIImageView(不建议这样做)。


更新:将UIImageView放在标签后面并不被推荐,因为这样你就需要管理两个视图。但是,如果你必须要做一些只有UIImageView才能做到的事情,那么这仍然是一个不错的方法。我猜测用这种方式运行你的应用程序实际上会更好。


2
优秀且实用的功能,棒极了!:) 但是我在想苹果在实现图像作为背景颜色时是怎么想的。这个设计完全不符合直觉!甚至连CSS都有专门控制背景颜色和图像的属性。也许这是从苹果以前的用户界面中遗留下来的? - Henrik Erlandsson
4
测试过了,它不支持 PNG 透明度。可能有一些诀窍吗?我确实尝试在设置图像之前将 backgroundColor 设置为 clearColor,只是出于好玩。但是没有效果,当然。 - Henrik Erlandsson
12
要使用透明的PNG图片,您需要将UILabel的不透明属性设置为NO:[theLabel setOpaque:NO]; - Dreamwieber
4
我可以,UILabel控件后面为什么不推荐使用UIImageView?谢谢。 - Azat
1
我在某处读到,使用colorWithPatternImage会对性能产生影响。 - pixelfreak
显示剩余5条评论

18

如果您想让图像被拉伸以填满标签的宽度。 尝试这段代码。

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

它能够工作,但质量不如预期。为什么会这样? - Esqarrouth
@Esq 如果你拉伸一张图片,它会失去清晰度,看起来不如原始图片高质量。 - Rob Evans
九宫格图片怎么样?这可以解决质量问题吗? - byJeevan
关于质量,只需使用UIGraphicsBeginImageContextWithOptions(_, _, 0)即可(最后一个参数为图像比例-> 0 = 自动)。 - Vagner

0

Swift 2.2:设置带有背景颜色的背景图像

    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
 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
        }

-1

@pixelfreak,你说得对。使用带有颜色图案的UIColor实例在分配给背景颜色时会出现严重的性能问题。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接