Change label height in tableview in iOS 7

1.6k views Asked by At

I have a label in custom cell in table view. I want to change cell height and also label height(at first I am showing only 2 line, I want to show more text) after user taped a cell,

I am able to change cell height based on cell content, but unfortunately I cannot change label height, :(( I've read tons of stackoverflow answers, but still nothing.

it is believed that this would work:

CGSize labelSize = [@"Hello World!" sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(label.frame.size.width,MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

but this is deprecated in iOS 7; can any one please help me in resizing my label in cell?

3

There are 3 answers

1
simonnickel On BEST ANSWER

If you want the height of your label relative to the height of your cell and you use Storyboard, you should determine the size of your label with constraints in relation to the ContentView of the cell. Just set the top and bottom distance from your label to the ContentView and no additional height for the label.

If you dont know how to set Storyboard and constraints, this might help: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html

0
nerowolfe On

You may use something like this to get UIlabel size

-(CGFloat)getLabelSize:(UILabel *)label fontSize:(NSInteger)fontSize
{


    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIFont systemFontOfSize:fontSize], NSFontAttributeName,
                                      nil];

    CGRect frame = [label.text boundingRectWithSize:CGSizeMake(270, 2000.0)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:attributesDictionary
                                        context:nil];

    CGSize size = frame.size;

    return size.height;
}
0
Gianluca Tranchedone On

UITableViewCell lays out it's content in -layoutSubviews. Therefore, if you want to simply have a custom layout logic, you need to subclass UITableViewCell and override the -layoutSubviews method. It's a good idea to call super anyway before applying your logic, though.