鉴于以下UITableViewCell代码,我们如何使myObj.firstName
红色和myObj.lastName
绿色?UITableViewCell中的多种颜色
UITableViewCell *cell;
....
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName];
鉴于以下UITableViewCell代码,我们如何使myObj.firstName
红色和myObj.lastName
绿色?UITableViewCell中的多种颜色
UITableViewCell *cell;
....
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", myObj.firstName, myObj.lastName];
你将需要UILabel
两个实例来实现这一点,因为UILabel
不支持的NSAttributedString
绘制实例。您也可以继承UITableViewCell
,覆盖drawRect:
,并使用NSString上的-drawTextAtPoint:
方法手动绘制带有所需颜色的文本。
或许,这可以帮助别人,我已经......
- (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];
}
颜色需要动态地根据数据集解决了这个。这仍然是可以在一个子类中工作的东西吗? – jwhat