DBoyer
DBoyer

Reputation: 3122

Header/Footer height of a UITableView section based on title text height

I am wondering if its possible to make the height header/footer for a section of a UITableView be equal to the height of the header/footer title text. Any tips would be great!

Note: some sections of my TableView may not have a header/footer and in that case would just have the padding between sections since the "headerTitleHeight/footerTitleHeight" would be zero in this case.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return headerTitleHeight + padding;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return footerTitleHeight + padding;
}

Upvotes: 10

Views: 24596

Answers (7)

Markicevic
Markicevic

Reputation: 1045

You can you automatic dimensions but you have to have estimated size. It works for Footer and Header the same way.

func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
    return 300
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}

Upvotes: 3

guywithmazda
guywithmazda

Reputation: 767

You can just

return UITableViewAutomaticDimension;

EDIT: Oops, ignore the following, see comments below.

or

return UITableViewAutomaticDimension + padding;

Upvotes: 5

Linh
Linh

Reputation: 60913

In viewDidLoad, heightForFooterInSection and heightForHeaderInSection

- (void)viewDidLoad {
   [super viewDidLoad];

   self.tableView.estimatedSectionFooterHeight = 50; // for footer
   self.tableView.estimatedSectionHeaderHeight = 50; // for header
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return UITableViewAutomaticDimension;
}

In Footer/Header xib file
- Add BOTH constraint top and bottom to ContainerView for your UILabel
- Add UILabel constraint height then change UILabel constraint height from Equal-> Greater than or Equals
- Finaly change UILabel line to 0

enter image description here

Upvotes: 0

RAJA
RAJA

Reputation: 1214

You can try the below..

CGSize constraint = CGSizeMake(<header/footer width>, <max header/footer height>);

CGSize size;

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
    NSRange range = NSMakeRange(0, [<string> length]);

    NSDictionary *attributes = [<string> attributesAtIndex:0 effectiveRange:&range];
    CGSize boundingBox = [<string> boundingRectWithSize:constraint options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
}
else
{
    size = [<string> sizeWithFont:descriptionLabel.font constrainedToSize:constraint lineBreakMode:descriptionLabel.lineBreakMode];
}

return size.height + padding;

or

CGSize maximumLabelSize = CGSizeMake(<header/footer width>, <max header/footer height>);

CGSize expectedSize = [<string label> sizeThatFits:maximumLabelSize];

return expectedSize.height + padding;

Upvotes: 0

Zeeshan
Zeeshan

Reputation: 4244

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{

    NSString *myHeader = [sectionsArray objectAtIndex:section];
    CGSize maxSize = CGSizeMake(320, 999999.0);
    int height = 0;

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];

    if (IS_IOS_6) //Macro i made for if iOS 6
    {
        CGSize size = [myHeader sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(15)]
                             constrainedToSize:maxSize
                                 lineBreakMode:NSLineBreakByWordWrapping];
        height = size.height;
    }
    else // IOS 7 , Attributes
    {
        CGRect frame = [myHeader boundingRectWithSize:maxSize
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:attributesDictionary
                                                context:nil];
        height = frame.size.height;
    }

    return height+5;
}

Upvotes: 13

NANNAV
NANNAV

Reputation: 4901

Calculate size of text and add padding height

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
CGFloat headerTitleHeight = [jobTitleString sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:20.0f] constrainedToSize:CGSizeMake(width,9999)].height;
    return headerTitleHeight + padding;
}

Upvotes: 1

Praveen S
Praveen S

Reputation: 10395

You could use boundingRectWithSize method to get the height required to draw the particular NSString for the header/footer and then return the values in the UITableView delegate methods. For sections without header footer, you have to code to just return the padding.

Upvotes: 0

Related Questions