添加UILabel作为隐藏图像视图图像的UIImageView的子视图

时间:2015-03-05 06:44:28

标签: uiimageview uilabel

UIImageView *newView =[[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 15.0f, 15.0f)];
    [newView setImage:image];

    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 15.0f, 15.0f)];
    newLabel.translatesAutoresizingMaskIntoConstraints = NO;
    newLabel.textAlignment = NSTextAlignmentCenter;
    newLabel.lineBreakMode = NSLineBreakByClipping;
    newLabel.adjustsFontSizeToFitWidth = YES;
    newLabel.text = labelText;
    newLabel.backgroundColor = [UIColor clearColor];
    [newView addSubview:newLabel];

1 个答案:

答案 0 :(得分:0)

我在你的代码中添加了两行并修改了一行,希望它对你有帮助..

UIImageView *newView =[[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 150.0f)];
[newView setImage:[UIImage imageNamed:@"3.png"]];//Modified line
[self.view addSubview:newView];//New added line

UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 150.0f)];
newLabel.translatesAutoresizingMaskIntoConstraints = NO;
newLabel.textAlignment = NSTextAlignmentCenter;
newLabel.lineBreakMode = NSLineBreakByClipping;
newLabel.adjustsFontSizeToFitWidth = YES;
newLabel.text = @"labelText";
newLabel.backgroundColor = [UIColor clearColor];
[newView addSubview:newLabel];

如果你的newLabel文字很长,你可以添加它..

newLabel.numberOfLines = 3;//New added line