【发布时间】:2010-08-04 21:39:36
【问题描述】:
我想在按钮图像上显示一个数字,例如大小为 30*28 的徽章,
我在按钮图像的顶部设置了一个徽章图像.. 在徽章图像之上,我应该能够显示文本或数字,并且我的徽章尺寸为 30*28。
所以,为了实现这一点,我想在我的按钮图像顶部设置一个标签,因此我想将标签背景设置为一些称为徽章图像的图像。
【问题讨论】:
我想在按钮图像上显示一个数字,例如大小为 30*28 的徽章,
我在按钮图像的顶部设置了一个徽章图像.. 在徽章图像之上,我应该能够显示文本或数字,并且我的徽章尺寸为 30*28。
所以,为了实现这一点,我想在我的按钮图像顶部设置一个标签,因此我想将标签背景设置为一些称为徽章图像的图像。
【问题讨论】:
在堆栈溢出本身中发现了这一点,因为上面的代码只是给出了没有标签的背景
对我有用 所以分享
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blah"]];
【讨论】:
您不能将背景图像添加到 UILabel,但您可以将 UIImageView 作为子视图添加到 UILabel。确保 UILabel 具有 backgroundColor = [UIColor clearColor]; 以提高透明度。
例如
UIImageView *labelBackground = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:@"mybg.png"]];
[myLabel addSubview:labelBackground];
[labelBackground release];
myLabel.backgroundColor = [UIColor clearColor];
应该可以。未经测试。
【讨论】:
要使图像适合您的 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];
【讨论】: