DaynaJuliana
DaynaJuliana

Reputation: 1164

UITableViewCell fit textLabel to entire frame

I'm trying to get my UITableViewCell.textLabel property to autofit text both vertically and horizontally.

Right now my tableView is not scrollable, so the cell frame is not dynamic. I need the textLabel property to word wrap and resize to a set # of lines. Setting the numberOfLines to 0 isn't working because it cut off because my cell height is smaller than the textLabel final height. Using an arbitrary number for numberOfLines cuts off the text.

How can I resize my textLabel to fit within the frame of the cell?

Code below

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell* cell = (UITableViewCell*)[tableView @"mycell"];

    cell.textLabel.text = @"LOTS OF TEXT";

    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    [cell.textLabel textRectForBounds:cell.bounds limitedToNumberOfLines:0];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.adjustsFontSizeToFitWidth = YES;
    cell.textLabel.minimumScaleFactor = .5;
}

Upvotes: 0

Views: 267

Answers (1)

Mike Sand
Mike Sand

Reputation: 2770

EDIT: If the cells are a set height this is straightforward. You will have to set a custom cell and textLabel in it, as I don't think you can't modify the textLabel in the basic or other preset cells the way you need to. Just add a text label and set constraints linking it to the four sides, with the minimum amount (can be zero). Labels automatically center text vertically if there is extra space, so making the label too tall shouldn't be a problem.

Original answer:


If you're not using tableView self-sizing cells, maybe to support iOS 7, then it won't matter what you do in the cell because the issue is in the tableView row heights. If a single label the classic boundingRect method will work. in tableView:heightForRowAtIndexpath: do

    NSString *stringFromDataSource = @"Text For Label";
    CGSize potentialSize = CGSizeMake(CGRectGetWidth(self.tableview.frame), CGFLOAT_MAX);
    NSStringDrawingOptions optionsForMultiline = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    NSDictionary *attributes = @{
                             NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody]
                             }; //or whatever
    CGRect rectForText = [stringFromDataSource boundingRectWithSize:potentialSize
                                     options:optionsForMultiline
                                  attributes:attributes
                                     context:nil];
    return CGRectGetHeight(rectForText);

Note that in your question, your various options for sizing the cell aren't going to be used if the textLabel is expanding with autolayout, unless there is a constraint capping the maximum size, at which point minimumScaleFactor and so on will kick in.

Upvotes: 1

Related Questions