开发者

setting an image in background of a label programmatically

开发者 https://www.devze.com 2023-01-04 23:39 出处:网络
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. How to do it programm开发者_如何学Catically?The si

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. How to do it programm开发者_如何学Catically?


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

精彩评论

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