2

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

1
  • 2
    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) Sep 10, 2013 at 0:08

4 Answers 4

5

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];
}
1
  • Or, instead of using sizeToFit, one could simply use CGMakeRect(0, count, desiredWidth, 20).
    – Hot Licks
    Sep 10, 2013 at 11:49
3

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

Cheers,

Happy Coding.

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.