¿Cómo crear uilabel dinámicamente desde un 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

preguntado el 10 de septiembre de 13 a las 01:09

Your problem is the frame you are giving the label. The width is zero, so you are not going to see it. (You're also putting them all in the exact same position) -

4 Respuestas

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

Respondido el 10 de Septiembre de 13 a las 01:09

Or, instead of using sizeToFit, one could simply use CGMakeRect(0, count, desiredWidth, 20). - Lamidas calientes

As suggested by rmaddy...adding a new line to adjust the height of the label as well, assumed that you have an NSMutableArray object 'items' containing strings.

float previousLabelHeight = 0.0;
for(int i = 0; i < [items count]; i++){
       CGSize theSize = [[items objectAtIndex: i] sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(320, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap]; //can adjust width from 320 to whatever you want and system font as well
       float newLabelHeight = previousLabelHeight + theSize.height;
       UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,newLabelHeight,0,0)];
       label.text = [items objectAtIndex: i];
       [label sizeToFit]; // resize the width and height to fit the text
       previousLabelHeight = newLabelHeight + 5 //adding 5 for padding

       [_scroll addSubview:label];
}

Aclamaciones,

Codificación feliz.

Respondido el 10 de Septiembre de 13 a las 12:09

I think you're trying to change the frame's Y value, but the last parameter of CGRectMake() is the rect's height. You want the second parameter.

Respondido el 10 de Septiembre de 13 a las 01:09

Here is the Swift version to add labels dynamically from array..

    var previousLabelHeight: CGFloat = 0.0;
    for dict in items {
        let text: String = "Some text to display in the UILabel"
        let size = heightNeededForText(text as NSString, withFont: UIFont.systemFontOfSize(15.0), width: scrollView.frame.size.width - 20, lineBreakMode: NSLineBreakMode.ByWordWrapping)
        let newLabelHeight = previousLabelHeight + size;
        let label =  UILabel(frame: CGRectMake(0, newLabelHeight, 0, 0))
        label.text = text
        label.sizeToFit() // resize the width and height to fit the text
        previousLabelHeight = newLabelHeight + 5 //adding 5 for padding
        scroll.addSubview(label)
    }

As sizeWithFont: ConstraintedToSize is deprecated from ios 7.0, we have to use boundingRectWithSize method from NSString....

func heightNeededForText(text: NSString, withFont font: UIFont, width: CGFloat, lineBreakMode:NSLineBreakMode) -> CGFloat {
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = lineBreakMode
    let size: CGSize = text.boundingRectWithSize(CGSizeMake(width, CGFloat.max), options: [.UsesLineFragmentOrigin, .UsesFontLeading], attributes: [ NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle], context: nil).size//text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MA

    return ceil(size.height);
}

contestado el 21 de mayo de 16 a las 08:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.