如何为标签设置背景图像

发布于 2024-09-12 12:45:47 字数 174 浏览 7 评论 0原文

我想在图像上显示一个数字,例如大小为 30*28 的徽章,将其放置在按钮图像上,

我有一个徽章图像要设置在按钮图像的顶部。 在徽章图像顶部,我应该能够显示文本或数字,并且我的徽章尺寸为 30*28。

因此,为了实现此目的,我想在按钮图像顶部设置一个标签,因此我想将标签背景设置为某个名为徽章图像的图像。

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 achieve 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝不放开 2024-09-19 12:45:48

在堆栈溢出本身中发现了这一点,因为上面的代码只给出了没有标签的背景,

这对我很有用
所以分享

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

请参阅:向 UILabel 添加背景图像

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

街角卖回忆 2024-09-19 12:45:48

您无法将背景图像添加到 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];

应该有效。未经测试。

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.

溺深海 2024-09-19 12:45:48

要使图像适合您的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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文