2011-12-17 5 views
0

次のUITableViewCellコードが与えられた場合、myObj.firstNameは赤、myObj.lastNameは緑色にするにはどうすればよいですか?UITableViewCellの複数の色

UITableViewCell *cell; 
.... 
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName]; 

答えて

0

あなたはUILabelNSAttributedStringの描画インスタンスをサポートしていないため、これを達成するためにUILabelの2つのインスタンスを必要とするつもりです。 UITableViewCellをサブクラス化し、drawRect:をオーバーライドし、テキストをNSStringの-drawTextAtPoint:メソッドで必要な色で手動で描画することもできます。

+0

でこれを解決してきた、他人を助けることがあります。これはサブクラスでも使えるものなのでしょうか? – jwhat

1

cell.textlabelをOHAttributedLabelインスタンスに設定できます。

OHAttributeLabel @ GitHub

+0

これはios5では動作しないようです。 CoreTextフレームワークを追加しても、多くのエラーが発生します。 – jwhat

0

おそらくこれは、私は色が動的データに応じて設定する必要があります...

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = [tv dequeueReusableCellWithIdentifier:@"Cell"]; 

    ... 

    // Get the size of UILabel firstName. 
    CGSize size = [[cell.firstName text] sizeWithFont:[cell.firstName font]]; 

    // Draw UILabel lastName next to UILabel firstName. 
    // UILabel firstName was initialized in CustomCell.m in -(void)layoutSubviews. 
    cell.lastName.frame = CGRectMake(size.width + 50, 23, 40, 15); 

    // Make lastName green, oh yeah! 
    cell.lastName.textColor = [UIColor colorWithRed:0 green:0.6 blue:0 alpha:1]; 
} 
関連する問題