0

I am trying to wrap my section header and UILineBreakModeWordWrap is not helping. Any idea what I am doing wrong?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{ 
    UIView *rOView = [[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] ;   
    UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    sectionLabel.backgroundColor = [UIColor clearColor];
    sectionLabel.font = [UIFont boldSystemFontOfSize:18];
    sectionLabel.frame = CGRectMake(70,18,200,20);
    sectionLabel.text =  @"A really really long text A really really long text A really really long text";
    sectionLabel.textColor = [UIColor blueColor];
    sectionLabel.numberOfLines = 0;
    sectionLabel.lineBreakMode = UILineBreakModeWordWrap;

    [roView addSubview:sectionLabel];
    return roView; 
}
user1509593
  • 1,025
  • 1
  • 10
  • 20

1 Answers1

0

You've not given the label a meaningful frame - only CGRectZero. After setting up your label, call -sizeToFit on it, like so:

UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectZero];
sectionLabel.backgroundColor = [UIColor clearColor];
sectionLabel.font = [UIFont boldSystemFontOfSize:18];
sectionLabel.frame = CGRectMake(70,18,200,20);
sectionLabel.text =  @"A really really long text A really really long text A really really long text";
sectionLabel.textColor = [UIColor blueColor];
sectionLabel.numberOfLines = 0;
sectionLabel.lineBreakMode = UILineBreakModeWordWrap;
[sectionLabel sizeToFit];

Edit: I see now that you did actually set the label frame. But the height is too short to display all the text.

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
  • Thank You. It worked. But for cell.textlabel, I didnot call the method sizeToFit, but is working. Is it because it is a textLabel? and textLabel and UILabel is little different. Appreciate your inputs. – user1509593 Aug 27 '12 at 01:39
  • Maybe the cell is sizing its label behind the scenes. You assigned your section label a height of 20, which is too little to display the three lines of text you needed. – Carl Veazey Aug 27 '12 at 01:43
  • Yup. Fixed it. I reduced the bottom (x,y) of the rectangle and top right is calculated based on tableView.bounds.size.width-10 since my section contents varies. Thanks again – user1509593 Aug 27 '12 at 01:56