views:

252

answers:

2

I have a UITableView, to which I add a Header-View, designed in InterfaceBuilder. This Header-View has two UILabels. The first label takes a title, that is always present. The second one displays a description, if present. Both strings can be quite long.

Is there a way to extend or shrink this labels and the view so that the text is show completely or no white-space will be shown?

- (void)viewDidLoad {
    //..... 
    if (self.tableHeaderView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ArticleHeader" owner:self options:nil];
        self.tableView.tableHeaderView = self.tableHeaderView;
        [(UILabel* )[self.tableView.tableHeaderView viewWithTag:1] setText:self.article.name];
        [(UILabel* )[self.tableView.tableHeaderView viewWithTag:2] setText:self.article.description];
    }
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [super viewDidLoad];
}

my solution (thanks to JK)

- (void)viewDidLoad {

    //....

    if (self.tableHeaderView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ArticleHeader" owner:self options:nil];
        UILabel *titleLabel = (UILabel* )[self.tableView.tableHeaderView viewWithTag:1]; 
        UILabel *descriptionLabel = (UILabel* )[self.tableView.tableHeaderView viewWithTag:2];

        titleLabel = (UILabel* )[self.tableHeaderView viewWithTag:1]; 
        descriptionLabel = (UILabel* )[self.tableHeaderView viewWithTag:2];

        CGSize sizeTitle = [self.article.name sizeWithFont:[titleLabel font]
                                        constrainedToSize:CGSizeMake(280.0, 700.0) 
                                            lineBreakMode:UILineBreakModeWordWrap];
        CGSize sizeDescription = [self.article.description sizeWithFont:[descriptionLabel font]
                                                          constrainedToSize:CGSizeMake(280.0, 700.0) 
                                                              lineBreakMode:UILineBreakModeWordWrap];

        float addHeight = 17.0;
        [self.tableHeaderView setFrame:CGRectMake(0, 0, 280, sizeTitle.height+addHeight + sizeDescription.height+addHeight)];
        [titleLabel setText:self.article.name];
        [descriptionLabel setText:self.article.description];

        [titleLabel setLineBreakMode:UILineBreakModeWordWrap];
        [titleLabel setFrame:CGRectMake(10,10, 260, sizeTitle.height)];

        [descriptionLabel setLineBreakMode:UILineBreakModeWordWrap];
        if (sizeDescription.height > 0) {
            [descriptionLabel setFrame:CGRectMake(20,
                                             titleLabel.frame.size.height+titleLabel.frame.origin.y, 
                                             260, 
                                             sizeDescription.height+addHeight)];
        } else {
            [descriptionLabel setHidden:YES];
        }

        self.tableView.tableHeaderView = self.tableHeaderView;
    }
    //.....
    [super viewDidLoad];
}

but i am curious why I need to set addHeight? Otherwise longer text will be truncated.

+1  A: 

You can use one of the methods in the NSString UIKit Additions Reference to determine the size of the labels for a given amount of text with a given font and line break mode. From this you can then determine the necessary height of the cell and adjust the cell height by implementing the UITableView delegate method tableView:heightForRowAtIndexPath:

Run Loop
A: 

[myTextView sizeToFit];

Dave DeLong