tags:

views:

18

answers:

1

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.

A: 

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.

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