开发者

How to set a background image to label

I want to display a number on image like badge of size 30*28, which is placed upon button image,

I have an badge image to set up on the top of button image.. on top of badge image I should able to display text or number and my badge size is 30*28.

so, for this to achiev开发者_JAVA技巧e I want to set a label on top of my button image and so I want to set label background to some image called badge image.


found this in stack overflow itself as the above code just gives the background without the label

was useful for me so sharing

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

See: Adding Background image to UILabel


You can't add a background image to a UILabel, but you can add a UIImageView as a subview to a UILabel. Make sure the UILabel has backgroundColor = [UIColor clearColor]; for transparency.

E.g.

UIImageView *labelBackground = [[UIImageView alloc] 
    initWithImage:[UIImage imageNamed:@"mybg.png"]];
[myLabel addSubview:labelBackground];
[labelBackground release];
myLabel.backgroundColor = [UIColor clearColor];

Should work. Untested.


To make the image fit in your UIview, try this code

UIImage *backgroundImage = [UIImage imageNamed:@"imageNameHere"];

UIGraphicsBeginImageContext(self.labelName.frame.size);
[backgroundImage drawInRect:CGRectMake(0, 0, self.labelName.frame.size.width, self.labelName.frame.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.labelName.backgroundColor = [UIColor colorWithPatternImage:newImage];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜