ios 将背景图像添加到 UILabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3037902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Background image to UILabel
提问by Joy
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.
如何将背景图像添加到 iPhone 应用程序中的 UILabel。我试图通过 IB 来做到这一点,但没有结果。
回答by Evadne Wu
Try doing it with code:
尝试用代码来做:
Objective-C:
目标-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).
或者UIImageView
在标签后面放置一个(不推荐)。
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.
更新:不建议将 UIImageView 放在标签后面,因为这样您就必须管理两个视图。但是如果你必须做一些只有 UIImageView 可以做的事情,这仍然是一个很好的方法。我怀疑您的应用程序实际上会以这种方式运行得更好。
回答by Lê Trinh
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];
回答by Bill Chan
Swift 2.2: set the Background image with background color
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)
回答by Davender Verma
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
}
回答by Ikechukwu Henry Odoh
@pixelfreak, your right. UIColor instance with color image pattern has serious performance issues when assigning it to backroundcolor
@pixelfreak,你的权利。具有彩色图像图案的 UIColor 实例在将其分配给背景色时存在严重的性能问题
cell.cellLabel.backgroundColor = UIColor(patternImage: UIImage(named: "background")!)