3

So I am making an app using iOS 6 and was wondering why my code that used to work great on iOS 5 does not work anymore. I have a cell with dynamic UILabel that gets adjusted based on the text that it carries.

This is with autolayout turned on:

enter image description here

This is with autolayout turned off:

enter image description here

Here is my code:

- (CGFloat)heightForText:(NSString *)bodyText
{

#define FONT_SIZE 13.0f
#define CELL_CONTENT_WIDTH 280.0f
#define CELL_CONTENT_MARGIN 5.0f

    CGSize constraintSize = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize labelSize = [bodyText sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat height = MAX(labelSize.height, 36.0f);
    //   NSLog(@"height=%f", height);
    return height + (CELL_CONTENT_MARGIN * 2);

}



- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger section = [indexPath section];

    switch (section) {


        case 0:
            if ([self.text.text length]!=0)
            {
                return [self heightForText:self.text.text];
            }
            else if ([self.link.text length]!=0)
            {

               return 60.0f;

            }
}
kratos
  • 2,465
  • 2
  • 27
  • 45
  • autolayout with a combination of Menu Bar -> Editor -> Size to fit content and/or "content hugging priority" in the "size inspector" inside Utilities (without other code for the label itself) doesn't work? – blub Feb 17 '13 at 15:49
  • if not: use outlets for width and height like http://stackoverflow.com/questions/13105544/auto-sizing-uilabel-when-using-autolayout – blub Feb 17 '13 at 15:59
  • I do not know how to use outlets for height weight constraints as stated in that question. – kratos Feb 17 '13 at 16:01
  • Menu Bar -> Editor -> Size to fit content seems to work. Will I have to do that from now on? What does that do? – kratos Feb 17 '13 at 16:09
  • just CTRL + drag from the contstraints to the interface ! – blub Feb 17 '13 at 16:10
  • you don't have to use autolayout if you don't want to, but if you do, http://www.raywenderlich.com/20881/beginning-auto-layout-part-1-of-2 is a great tutorial for autolayout (introduction) in general ! – blub Feb 17 '13 at 16:32
  • Check this http://stackoverflow.com/a/16009707/988169. This is working perfectly fine. – pkc456 May 25 '15 at 07:47

1 Answers1

2

The code you've shown is fine, the problem is most likely within the constraints you have on the label within your cell (which you don't show).

Your label needs to have constraints to make it a fixed distance from each edge of its superview. This may already be the case - if so, the problem is now that the label doesn't know how to wrap its lines - you need to also set the preferredMaxLayoutWidth on the label.

I have found that tools such as DCIntrospect are really helpful when debugging Autolayout issues. For example, in this situation, its hard to tell if this is a label of the right size that is not wrapping text, or if the label is not tall enough to show your content properly.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • 1
    Just to add: definitely do try setting `preferredMaxLayoutWidth` rather than constraining the label width using AutoLayout: it was indeed knocking the label onto two lines, but not pushing down elements beneath it. Very frustrating. – Ian Dundas May 31 '13 at 07:09