Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 901
iPhone NewBIe Avatar asked Aug 22 '12 11:08

iPhone NewBIe


4 Answers

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];
like image 91
Kuldeep Singh Avatar answered Oct 17 '22 11:10

Kuldeep Singh


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.

like image 28
Damitha Raveendra Avatar answered Oct 17 '22 11:10

Damitha Raveendra


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;
like image 36
Rupesh Avatar answered Oct 17 '22 11:10

Rupesh


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.

like image 22
iPhone NewBIe Avatar answered Oct 17 '22 11:10

iPhone NewBIe