Iphone UITableViewCell中的多种颜色
给定以下UITableViewCell代码,我们如何使Iphone UITableViewCell中的多种颜色,iphone,ios,uitableview,ios5,Iphone,Ios,Uitableview,Ios5,给定以下UITableViewCell代码,我们如何使myObj.firstName为红色和myObj.lastName为绿色 UITableViewCell *cell; .... cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName]; 您需要两个UILabel实例才能实现这一点,因为UILabel不支持NSAttributedString的图形实例。
myObj.firstName
为红色和myObj.lastName
为绿色
UITableViewCell *cell;
....
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName];
您需要两个
UILabel
实例才能实现这一点,因为UILabel
不支持NSAttributedString
的图形实例。您还可以使用NSString上的-drawTextAtPoint:
方法,将UITableViewCell子类化,重写drawRect:
,并使用所需颜色手动绘制文本。您可以尝试将cell.textlabel设置为OHAttributedLabel实例
也许这对其他人有帮助,我已经用
- (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];
}
颜色需要根据数据动态设置。这仍然可以在子类中工作吗?这似乎不适用于ios5。许多错误,甚至添加了CoreText框架。