Sebastian Flückiger
Sebastian Flückiger

Reputation: 5555

resize title in uitableviewcellstyledefault

i've been trying around for a while now and cant seem to find a solution. i am using a UITableViewCellStyleDefault cellstyle in my tableview, and am trying to get the font to resize when the text gets too long.

cell creation

static NSString *CellIdentifier = @"thisMonthCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    [cell.textLabel setTextColor:[UIColor darkGrayColor]];
    [cell.textLabel setAdjustsFontSizeToFitWidth:YES];
    [cell.textLabel setMinimumFontSize:14];

    UILabel *valueLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 10, 100, 20)];
    [valueLabel setBackgroundColor:[UIColor clearColor]];
    valueLabel.tag = 1001;
    [valueLabel setTextAlignment:UITextAlignmentRight];
    [cell addSubview:valueLabel];

}

Expense *expense = [[self.dataHandler monthExpenses]objectAtIndex:indexPath.row];

UILabel *value = (UILabel*)[cell viewWithTag:1001];
[cell.textLabel setText:[expense name]];

if ([[expense value]doubleValue] > 0) {
    [value setText:[NSString stringWithFormat:@"+%.2f",[[expense value]doubleValue]]];
    [value setTextColor:[self greenColor]];    

}
else{
    [value setText:[NSString stringWithFormat:@"%.2f",[[expense value]doubleValue]]];
    [value setTextColor:[self redColor]];    
}
return cell;

but somehow the textLabel wont resize if the text is too long.

here is a screenshot demonstrating the problem:

autosize fail

any ideas?

UPDATE i managed to achieve my goal by removing the standardLabel and adding a custom one,.. weird that it would not work with the standard one.

Upvotes: 1

Views: 1502

Answers (2)

rakeshNS
rakeshNS

Reputation: 4257

Try this cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap

Upvotes: 2

rishi
rishi

Reputation: 11839

Use following two line of code also when you are creating value label.

valueLabel.lineBreakMode = UILineBreakModeWordWrap;
valueLabel.numberOfLines = 0;

EDITED- To change the height of cell -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellText = @"oooooooooooooooooooo"; //Text that you are using
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:16.0]; //Whatever font you are using.
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 25.0; //25.0 is offset, you can change as per need

}

Upvotes: 0

Related Questions