2011-12-17 72 views
0

鉴于以下UITableViewCell代码,我们如何使myObj.firstName红色和myObj.lastName绿色?UITableViewCell中的多种颜色

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

回答

0

你将需要UILabel两个实例来实现这一点,因为UILabel不支持的NSAttributedString绘制实例。您也可以继承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]; 
}