2011-12-27 77 views
3

如何实现以下效果?如何在图像中实现背景图像在UILabel中?

我希望当我的UIImage宽度小于UILabel宽度时,我的图像应该只显示一次,如下所示。

我已经用UILabelUIImage做了一些更多的工作。参考我以前的问题:How to stretch Image to fill the Label Width set in Background in UILabel?

但现在我希望有更多的乐趣与UILabel ...:d

UILabel with small Image than width repeats only once...

编辑:

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

回答

0
if (image.size.width < label.size.width ||image.size.height < label.size.height) 
{ 
//rect here is frame of image 
    [img drawInRect:rect]; 
    label.backgroudColor = [UIColor clearColor]; 
} 
+0

我想图像不是作为不同的控制,但作为标签的背景... – DShah 2011-12-27 17:14:00

0

这似乎是最直接的解决办法是让您的UIImage坐在您的UILabel后面,并且您的UILabel具有透明背景色。

编辑

假设你正在使用ARC ...

SBLabel.h

#import <UIKit/UIKit.h> 

@interface SBLabel : UILabel 

@property (nonatomic, strong) UIImageView *nonRepeatingBackgroundImage; 

@end 

SBLabel.m

#import "SBLabel.h" 

@implementation SBLabel 

@synthesize nonRepeatingBackgroundImage; 

- (void)setNonRepeatingBackgroundImage:(UIImageView *)aNonRepeatingBackgroundImage { 
    nonRepeatingBackgroundImage = aNonRepeatingBackgroundImage; 
    [self addSubview:aNonRepeatingBackgroundImage]; 
    [self sendSubviewToBack:aNonRepeatingBackgroundImage]; 
    [self setBackgroundColor:[UIColor clearColor]]; 
} 

@end 

将UILabel添加到父视图后,您需要调用此setter方法。我没有测试,但我相信它应该可以工作,可能需要一些调整,但希望它解释了如何子类UILabel作出这样的自定义增强。

+0

这可能是坏的解决方案,如果我把它....因为有数百UILabel那么它将很难处理所有的标签和图片...所以我想图像作为UILabel的背景.... – DShah 2011-12-27 17:18:21

+0

我建议子类化UILabel来处理这种类型的背景图像,如果是这种情况。 – 2011-12-27 17:41:11

+0

你能帮我做分类吗?或至少描述如何实现这一目标? – DShah 2011-12-27 18:00:44

0

的UILabel的一个子类可能看起来像这样(没有ARC)

CustomLabel.h

#import <UIKit/UIKit.h> 

@interface CustomLabel : UILabel { 
    UIImage *mImage; 
} 
@property (retain) UIImage *image; 
@end 

CustomLabel.m

#import "CustomLabel.h" 

@implementation CustomLabel 

- (void)dealloc { 
    self.image = nil; 
    [super dealloc]; 
} 

- (void)drawRect:(CGRect)rect { 
    [mImage drawAtPoint:CGPointMake(0, 0)]; 
    [super drawRect:rect]; 
} 

- (UIImage*)image { 
    return mImage; 
} 

- (void)setImage:(UIImage *)image { 
    self.backgroundColor = [UIColor clearColor]; 

    if(mImage) 
     [mImage release]; 
    mImage = [image retain]; 
    [self setNeedsDisplay]; 
} 
@end 

使用

yourCustomLabel.image = [UIImage imageNamed:@"test.png"]; 
+0

我已经尝试过这个同样的事情......但没有获取图像在背景中......我应该从哪里调用'drawRect'方法?如何调用setImage?你已经提供了使用语法,但没有包含那...我认为还有一些东西仍然缺少,我无法弄清楚......请帮我弄清楚...... – DShah 2011-12-28 05:33:14

+0

你不要调用drawRect,渲染系统在调用setNeedsDisplay之后调用它(当调用setImage:时)。这里调用setImage:'yourCustomLabel.image = [UIImage imageNamed:@“test.png”];' – vakio 2011-12-28 09:32:15

+0

我已经创建了一个示例项目。并像评论它的每一行。 [链接](http://weezer.random-host.com/eternity/CustomLabelExample.zip) – weezor 2011-12-28 13:12:24