简体   繁体   中英

Adding Labels As subview to UIView

can anyone tell me how to add two labels for a view that i was added on UITableView Cell.i have created that view as UIView with some name.and i have created two labels in UIView class and also set frame for labels,set text and etc.my problem is am getting that view in tableview cell but not those labels.

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

this is just i am setting frame,text to labels like that in a 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];

and here i am adding that UIView to a tableview cell.and i dont know how to add those labels to my UIView. please help me.

sorry if any mistakes in english. anyone please help me. thanks alot in advance.

create labels like label and label1 and add in 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];

Add the labels to GreeView like this,

Eg:

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

Hope this will help you.

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

Finally i Got my answer as above. Thanks alot for all ur replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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