Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewCell中的多种颜色_Iphone_Ios_Uitableview_Ios5 - Fatal编程技术网

Iphone 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的图形实例。

给定以下UITableViewCell代码,我们如何使
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框架。