以编程方式在标签的背景中设置图像

时间:2021-05-31 22:43:36

i have a label(many labels in fact)now i want an image in the background of my label(same image) so that text writtenin my label is shown to user.I have to do it programatically.... It'll be great if someone can guide me.Thanks.

我有一个标签(实际上有许多标签)现在我想在我的标签背景中找到一个图像(相同的图像),以便我的标签上写的文字显示给用户。我必须以编程方式进行....它将是如果有人可以指导我,那就太棒了。谢谢。

1 个解决方案

#1


0  

The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.

最简单的方法就是简单地在UILabel后面层叠UIImageView。如果你想使它更强大,你可以创建一个UILabel子类,它暴露一个方法或属性来设置图像背景,它会将图像添加到自身。

CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:@"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...

[self.view addSubview:theImageView];
[self.view addSubview:theLabel];

[theImageView release];
[theLabel release];

#1


0  

The simplest way would just be to simply layer a UIImageView behind the UILabel. If you wanted to make it more robust you could make a UILabel subclass that exposes a method or property to set the image background and it would add the image to itself.

最简单的方法就是简单地在UILabel后面层叠UIImageView。如果你想使它更强大,你可以创建一个UILabel子类,它暴露一个方法或属性来设置图像背景,它会将图像添加到自身。

CGPoint labelPos = CGPointMake(123, 234);
UIImage *theImage = [UIImage imageNamed:@"button_bg.png"];
UIImageView *theImageView = [[UIImageView alloc] initWithImage:theImage];
theImageView.frame = CGRectMake(labelPos.x, labelPos.y, theImage.size.width, theImage.size.height);
UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelPos.x, labelPos.y, 100, 50)];
theLabel.backgroundColor = [UIColor clearColor];
// ...label config...

[self.view addSubview:theImageView];
[self.view addSubview:theLabel];

[theImageView release];
[theLabel release];