如何在 UILabel 中实现如图所示的背景图像?
下面的效果如何实现??
我希望当我的 UIImage
宽度小于 UILabel
宽度时,我的图像应该只显示一次,如下所示。
我已经使用 UILabel
和 UIImage
完成了更多工作。请参阅我之前的问题:如何拉伸Image以填充UILabel中Background设置的Label宽度?。
但现在我想要更多乐趣 UILabel
... :D
编辑:
SBLabel *lbl = [[SBLabel alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
lbl.text = @"Hello World!!!...";
UIImage *img = [UIImage imageNamed:@"cn3.png"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
imgView.image = img;
[lbl setNonRepeatingBackgroundImage:imgView];
[self.view addSubview:lbl];
[imgView release];
How to achieve below effect??
I want that when my UIImage
width is smaller then UILabel
width then my image should be displayed only once as below.
I already have done some more work with UILabel
and UIImage
. Refer to my previous question : How to stretch Image to fill the Label Width set in Background in UILabel?.
But now i want some more fun with UILabel
... :D
EDIT :
SBLabel *lbl = [[SBLabel alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
lbl.text = @"Hello World!!!...";
UIImage *img = [UIImage imageNamed:@"cn3.png"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320, 28)];
imgView.image = img;
[lbl setNonRepeatingBackgroundImage:imgView];
[self.view addSubview:lbl];
[imgView release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
似乎最简单的解决方案是将您的
UIImage
放在UILabel
后面,并且您的UILabel
具有透明的背景色。编辑
假设您使用的是 ARC...
SBLabel.h
SBLabel.m
在添加 UILabel 后,您需要调用此 setter 方法到父视图。我还没有测试过,但我相信它应该可以工作,可能需要一些调整,但希望它解释了如何子类化 UILabel 来进行这样的自定义增强。
It seems like the most straightforward solution would be to have your
UIImage
sit behind yourUILabel
and yourUILabel
has a transparent background color.Edit
Assuming you're using ARC...
SBLabel.h
SBLabel.m
You would need to call this setter method after you add the UILabel to the parent view. I have not tested but I believe it should work, might require some tweaks but hopefully it explains how to Subclass UILabel to make a custom enhancement such as this.
UILabel 的子类可能如下所示(没有 ARC)
CustomLabel.h
CustomLabel.m
用法
A subclass of UILabel could look like this (without ARC)
CustomLabel.h
CustomLabel.m
Usage