ios:如何在背景图像上添加 uiLabel?

Posted

技术标签:

【中文标题】ios:如何在背景图像上添加 uiLabel?【英文标题】:ios : how to add uiLabel on top of the background image? 【发布时间】:2014-11-10 05:13:00 【问题描述】:

我正在尝试将 uiLabel 添加到作为背景图像的 uiImageVIew 之上。在 uiImageView 之上添加 uiLabel 时,在测试和运行时无法添加 uilabel。如果这样做,您能告诉我替代方案或预防措施吗?

以下是我的代码:

@implementation CoverViewController

- (void)viewDidLoad 
    [super viewDidLoad];

   _container = [[UIView alloc] initWithFrame:[self.view bounds]];

    [self setTapDemo :[UIImage imageNamed:@"cover_1_d.png"] ] ;




- (void) setTapDemo: (UIImage *) cover 

    _image = [UIImage imageNamed:@"shaded_region.png"];
    _imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [_imageShaded setImage:_image];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage: cover ];
    _imageShaded.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [_imageShaded addGestureRecognizer:myGesture];
    [_imageShaded setContentMode:UIViewContentModeScaleAspectFill];

    NSString * text = @"Quad name:";

    CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
    UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
    fromLabel.text = text;
    fromLabel.numberOfLines = 1;
    fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
    fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
    fromLabel.adjustsFontSizeToFitWidth = YES;
    fromLabel.minimumScaleFactor = 10.0f/12.0f;
    fromLabel.clipsToBounds = YES;
    fromLabel.backgroundColor = [UIColor clearColor];
    fromLabel.textAlignment = NSTextAlignmentLeft;
    [_imageShaded addSubview:fromLabel];
    [imageView addSubview:fromLabel];


    [_container addSubview:_imageShaded];
    [_container addSubview:imageView];
    [_container addSubview:fromLabel];

    [self.view addSubview:_container];
    [self.view sendSubviewToBack:_container];



【问题讨论】:

【参考方案1】:

检查一下

- (void) setTapDemo: (UIImage *) cover 

    _image = [UIImage imageNamed:@"images.jpg"];
    _imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
    [_imageShaded setImage:_image];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
    [imageView setImage: cover ];
    _imageShaded.userInteractionEnabled = YES;
    UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesBegan:)];
    myGesture.numberOfTapsRequired = 1;
    myGesture.delegate=self;
    [_imageShaded addGestureRecognizer:myGesture];
    [_imageShaded setContentMode:UIViewContentModeScaleAspectFill];

    NSString * text = @"Quad name:";
   // CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
    UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 300, 40)];
    fromLabel.text = text;
    fromLabel.textColor = [UIColor greenColor];
    fromLabel.numberOfLines = 1;
    fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
    fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
    fromLabel.adjustsFontSizeToFitWidth = YES;
    fromLabel.minimumScaleFactor = 10.0f/12.0f;
    fromLabel.clipsToBounds = YES;
    fromLabel.backgroundColor = [UIColor clearColor];
    fromLabel.textAlignment = NSTextAlignmentLeft;



    [_container addSubview:_imageShaded];
    [_container addSubview:imageView];


    [self.view addSubview:_container];
    [self.view sendSubviewToBack:_container];
    [imageView addSubview:fromLabel];



【讨论】:

【参考方案2】:

您在不同的视图上多次添加 UILabel。您应该一次将其添加到一个视图中;

//[_imageShaded addSubview:fromLabel];   // 1
//[imageView addSubview:fromLabel];      // 2


[_container addSubview:_imageShaded];
[_container addSubview:imageView];
//[_container addSubview:fromLabel];     //3

【讨论】:

以上是关于ios:如何在背景图像上添加 uiLabel?的主要内容,如果未能解决你的问题,请参考以下文章

iPhone iOS:如何在图像背景上动态移动/调整图像大小?

如何在不改变文本透明度的情况下为 UILabel 设置背景图像并调整其 alpha?

iOS - 以图像为背景归档 UILabel

如何在图像视图中添加背景图像以及我希望它显示在背景图像 ios 上的图像?

如何在 iOS 中仅缩进多行 UILabel 的第一行?

如何在 ios7 上将导航栏背景图像添加到 MFMailComposeViewController