2011-12-27 13 views
3

以下の効果を達成するには?どのように背景を達成するには画像のようにUILabelで?

UIImageの幅がUILabelの幅より小さいとき、私の画像は以下のように1回だけ表示されます。

私は既に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...

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

答えて

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

0

それが最も簡単な解決策のように思えることだろうあなたのUILabelとあなたのの背後にあなたのUIImageが座っているには透明な背景色があります。

編集

あなたと仮定すると、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を追加した後、このセッターメソッドを呼び出す必要があります。私はテストしていませんが、うまくいくはずですが、いくつかの調整が必要かもしれませんが、うまくいけばUILabelをサブクラス化してこのようなカスタム拡張を行う方法を説明してくれることを願っています。

+0

私はそれを取ると悪い解決策になるかもしれません...何百ものUILabelを持っているので、 Images ...だから私はUILabelの背景として画像を望んでいます.... – DShah

+0

この場合、このタイプの背景画像を扱うためにUILabelをサブクラス化することをお勧めします。 –

+0

サブクラス化を手伝ってもらえますか?またはatleastはそれを達成する方法を記述する?? – DShah

0

UILabelのサブクラスは

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 

使用法(ARCなし)このようになります。

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

これと同じことを試みましたが、バックグラウンドで....どこから 'drawRect'メソッドを呼び出すべきですか? setImageはどのように呼び出されますか?あなたは使用構文を与えましたが、それを含んでいません...私は何かがまだ分からないと思います...私はそれを理解するために助けてください... – DShah

+0

あなたdrawRectを呼び出さない、レンダリングsetNeedsDisplayが呼び出された後(setImage:が呼び出されたとき)に呼び出されます。 setImageはここで呼び出されます: 'yourCustomLabel.image = [UIImage imageNamed:@" test.png "];' – vakio

+0

私はサンプルプロジェクトを作成しました。そして、それのすべての一行をコメントしたように。 [link](http://weezer.random-host.com/eternity/CustomLabelExample.zip) – weezor

関連する問題