繁体   English   中英

添加标签作为UIView的子视图

[英]Adding Labels As subview to UIView

任何人都可以告诉我如何为我在UITableView上添加的视图添加两个标签Cell.i已经创建了具有一些名称的UIView视图。我在UIView类中创建了两个标签,还为标签设置了框架,设置了文本和我的问题是在tableview单元格中获取该视图但不是那些标签。

    countLabel.text = @"4";
    countLabel.frame=CGRectMake(275, 10, 20, 15);
    countLabel.font=[UIFont boldSystemFontOfSize:15.0];
    countLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
    countLabel.backgroundColor=[UIColor clearColor];

    hrsLabel.text = @"Hours";
    hrsLabel.frame=CGRectMake(260, 30, 45, 15);
    hrsLabel.font=[UIFont boldSystemFontOfSize:15.0];
    hrsLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
    hrsLabel.backgroundColor=[UIColor clearColor];

这只是我在UIView.and中设置框架,文本到标签

GreenView *greenView = [[GreenView alloc] initWithFrame:CGRectMake(250, 8, 60, 50)];
greenView.backgroundColor = [UIColor colorWithRed:0.000 green:0.690 blue:0.313 alpha:0.5];
[cell.contentView addSubview:greenView];

在这里我将UIView添加到tableview cell.and我不知道如何将这些标签添加到我的UIView。 请帮我。

抱歉,如果英语有任何错误。 有人请帮助我。 非常感谢。

创建label和label1之类的标签,并在UIView中添加

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 250, 15)];

[label setText:@"Hello"];

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 250, 15)];

[label1 setText:@"Hello1"];

UIView *myView = [UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

[myView addSubview:label];
[myView addSubview:label1];

像这样添加标签到GreeView

例如:

    GreenView *greenView = [[GreenView alloc] initWithFrame:CGRectMake(250, 8, 60, 50)];
    greenView.backgroundColor = [UIColor colorWithRed:0.000 green:0.690 blue:0.313 alpha:0.5];

    countLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 20, 15)];
    countLabel.text = @"4"; 
    countLabel.font=[UIFont boldSystemFontOfSize:15.0]; 
    countLabel.textColor=[UIColor whiteColor];
    countLabel.backgroundColor=[UIColor clearColor]; 
    [greenView addSubview:countLabel];

    hrsLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 45, 15)];
    hrsLabel.text = @"Hours";
    hrsLabel.font=[UIFont boldSystemFontOfSize:15.0]; 
    hrsLabel.textColor=[UIColor whiteColor]; 
    hrsLabel.backgroundColor=[UIColor clearColor];
    [greenView addSubview:hrsLabel];

    [cell.contentView addSubview:greenView];

希望这会帮助你。

countLabel.text = @"4";
countLabel.frame=CGRectMake(275, 10, 20, 15);
countLabel.font=[UIFont boldSystemFontOfSize:15.0];
countLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
countLabel.backgroundColor=[UIColor clearColor];

hrsLabel.text = @"Hours";
hrsLabel.frame=CGRectMake(260, 30, 45, 15);
hrsLabel.font=[UIFont boldSystemFontOfSize:15.0];
hrsLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
hrsLabel.backgroundColor=[UIColor clearColor];

    [greenView  addSubview: countLabel];
    [greenView  addSubview: hrsLabel];
    [cell.contentview addSubview:greenView];

    return cell;
countLabel.text = @"4";
countLabel.frame=CGRectMake(275, 10, 20, 15);
countLabel.font=[UIFont boldSystemFontOfSize:15.0];
countLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
countLabel.backgroundColor=[UIColor clearColor];

hrsLabel.text = @"Hours";
hrsLabel.frame=CGRectMake(260, 30, 45, 15);
hrsLabel.font=[UIFont boldSystemFontOfSize:15.0];
hrsLabel.textColor=[UIColor colorWithWhite:0.0 alpha:0.5];
hrsLabel.backgroundColor=[UIColor clearColor];

[self addSubView:countLabel];
[self addSubView:hrsLabel];

最后我得到了上面的回答。 非常感谢您的所有回复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM