2010-08-04 150 views
4

我想像尺寸30 * 28,其被放置在按钮图像的徽章上的图像显示的数字,如何设置背景图像标记

我有一个徽章图像来设置上的顶按钮图像.. 徽章图像上我应该能够显示文本或数字,我的徽章大小是30 * 28 ...

因此,为了实现我想在我的按钮上设置标签图像,所以我想设置标签背景的一些图像称为徽章图像

回答

10

无法将背景图片添加到一个UILabel,但您可以添加一个UIImageView作为子视图一个UILabel。确保UILabel的透明度为backgroundColor = [UIColor clearColor];

E.g.

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

应该工作。未经测试。

+0

但标签文本是不可见的。只有图像出现。 – prajul 2012-01-06 16:08:55

+1

错误的答案,背景图片可以添加到uilabels!不鼓励将UIImage添加为子视图以提供“背景”效果。 – 2012-11-05 13:22:09

+0

随意使用的措辞“过时的答案”的答案2年前是正确的,当它被张贴。 ;)也就是说,如果人们想要与旧版iOS兼容:它们可能仍然会发现这个答案很有用。 – Kalle 2012-11-07 16:25:56

10

看到这个堆栈溢出本身上面的代码只是给没有标签

对我来说 有用的,共享

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

看到背景:Adding Background image to UILabel

1

要使图像适合你的UIView试试这个代码

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