2012-04-20 31 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