I have a UITableView
, to which I add a Header-View, designed in InterfaceBuilder. This Header-View has two UILabel
s. 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.