xonegirlz
xonegirlz

Reputation: 8969

UILabel in UITableViewCell overflows

I have a UILabel inside a UITableViewCell and I was trying to adjust the height, however when the height is greater than the cell height it overflows to the next cell below it. How can I avoid this? I am adding this into my contentView:

 [self.contentView addSubview:self.commentsText_];

Upvotes: 0

Views: 415

Answers (2)

Nitin
Nitin

Reputation: 7461

Using following code you can calculate height of label and also change the height of cell

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath {  

    UILabel *yourlabel;// use your memober class UILabel. I am declare here temporary.
    CGSize s = [yourlabel.text sizeWithFont:[UIFont systemFontOfSize:15] // enter your text font size and  cell-width
                             constrainedToSize:CGSizeMake(yourcellwidth, MAXFLOAT)  // - 40 For cell padding
                                 lineBreakMode:UILineBreakModeWordWrap];    



    return s.height;    //this will give you height of UILabel view you can change using addition according your requirements
}

Hope, this will help you..

Upvotes: 3

Thant Thet
Thant Thet

Reputation: 196

if you want to hide the overflows.

self.contentView.clipsToBounds = YES;

or you may want to layout by overwriting at

- (void)setNeedsLayout
{
    [super setNeedsLayout];
    self.commentsText_.frame = .... // layout your label
}

Upvotes: 3

Related Questions