如何在 UILabel 中实现如图所示的背景图像?

发布于 2024-12-23 02:09:55 字数 896 浏览 0 评论 0原文

下面的效果如何实现??

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

我已经使用 UILabelUIImage 完成了更多工作。请参阅我之前的问题:如何拉伸Image以填充UILabel中Background设置的Label宽度?

但现在我想要更多乐趣 UILabel... :D

UILabel 图像比宽度重复小仅一次...

编辑:

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

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

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

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

发布评论

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

评论(3

一袭水袖舞倾城 2024-12-30 02:09:55
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];
}
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];
}
染年凉城似染瑾 2024-12-30 02:09:55

似乎最简单的解决方案是将您的 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 来进行这样的自定义增强。

It seems like the most straightforward solution would be to have your UIImage sit behind your UILabel and your UILabel has a transparent background color.

Edit

Assuming you're using 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

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.

掩饰不了的爱 2024-12-30 02:09:55

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

A subclass of UILabel could look like this (without 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

Usage

yourCustomLabel.image = [UIImage imageNamed:@"test.png"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文