2012-04-20 54 views
1

我一直在尝试一段时间,现在似乎找不到解决方案。我在我的tableview中使用了UITableViewCellStyleDefault cellstyle,并且在文本变得太长时,我试图调整字体的大小。在uitableviewcellstyledefault调整标题

细胞创作

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; 

但不知何故textLabel不会调整,如果文本太长。

这里是一个屏幕截图,展示问题:

autosize fail

什么想法?

更新我设法实现我的目标,通过删除standardlabel并添加一个自定义的,..奇怪的是,它不会与标准的工作。

回答

2

试试这个cell.textLabel.numberOfLines = 0;cell.textLabel.lineBreakMode = UILineBreakModeWordWrap

0

当您创建值标签时,请使用以下两行代码。

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

EDITED- 要更改单元格的高度 -

- (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 

}

+0

我加入他们 - 但它并没有改变任何东西(它的'textLabel'太长而不值得) – 2012-04-20 08:38:01

+0

valueLabel.numberOfLines = INT_MAX ..你可以用这个语句检查一次。 – rishi 2012-04-20 08:52:29

+0

那里没有变化,.. sry – 2012-04-20 08:58:36