Fabio Fabio - 6 months ago 17
iOS Question

how create uilabel's dynamically from an NSMutableArray?

NSMutableArray *items
// contains 15 items

I need to put one down label from another i try something like this but not work

int count=20;

for(int i = 0; i < [items count]; i++){
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(0,0,0,count)];
label.text = @"text"; //etc...
count+=20;

[_scroll addSubview:label];

}


What can i do thanks

Answer

You need to set the frame properly.

int count=20;

for(int i = 0; i < [items count]; i++){
    UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,count,0,0)];
    label.text = @"text"; //etc...
    [label sizeToFit]; // resize the width and height to fit the text
    count+=20;

    [_scroll addSubview:label];
}