2011-12-27 25 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