Iphone 如何使UITableViewCell具有两个不同颜色的标签?

Iphone 如何使UITableViewCell具有两个不同颜色的标签?,iphone,objective-c,uitableview,uilabel,Iphone,Objective C,Uitableview,Uilabel,我希望UITableViewCell如下所示: “Tel:”的文本颜色必须与数字的文本颜色不同。现在,我像往常一样将文本设置为单元格: cell.textLabel.text=@"Something"; 是否可以有一个标签,并改变它的某些部分的颜色 如何在我的图片上制作表格单元格 谢谢。唯一的方法是以不同颜色作为背景添加新标签作为子视图。可以使用自定义单元格并根据需要初始化,也可以使用UITableviewCell并在其中根据所需格式添加多个UILabel 例如 static NSStrin

我希望UITableViewCell如下所示:

“Tel:”的文本颜色必须与数字的文本颜色不同。现在,我像往常一样将文本设置为单元格:

cell.textLabel.text=@"Something";
是否可以有一个标签,并改变它的某些部分的颜色

如何在我的图片上制作表格单元格


谢谢。

唯一的方法是以不同颜色作为背景添加新标签作为子视图。

可以使用自定义单元格并根据需要初始化,也可以使用UITableviewCell并在其中根据所需格式添加多个UILabel

例如

static NSString *MyIdentifier =@"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) 
{       
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier];     
}

UILabel *lblPhone = [[UILabel alloc] initwithFrame:CGRectMake(5, 5, 150, 30)];
lblPhone.text = @"Tel.";
[cell addSubView: lblPhone];

无法更改UILabel部分的字体或颜色。这里最简单的方法是创建一个cell子类,并在接口生成器或代码中添加两个标签。演示如何计算标签中文本的大小,以便在希望两个标签的文本为“thouching”时可以动态调整标签的大小

您应该在单元格中添加两个标签和…并在UITableViewCell的子视图中添加这两个标签

在cellForRowAtIndexPath方法中写入此项

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"Cell";  
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    for(UIView *eachView in [cell subviews])
        [eachView removeFromSuperview];

    //Initialize Label
    UILabel *lbl1 = [[UILabel alloc]initWithFrame:YOURFRAME];
    [lbl1 setFont:[UIFont fontWithName:@"FontName" size:12.0]];
    [lbl1 setTextColor:[UIColor grayColor]];
    lbl1.text = YOUR CELL TEXT;
    [cell addSubview:lbl1];
    [lbl1 release];

    UILabel *lbl2 = [[UILabel alloc]initWithFrame:YOURFRAME];
    [lbl2 setFont:[UIFont fontWithName:@"FontName" size:12.0]];
    [lbl2 setTextColor:[UIColor blackColor]];
    lbl2.text = YOUR CELL TEXT;
    [cel2 addSubview:lbl2];
    [lbl2 release];

    return cell;
}
更新:

除此之外,您还可以创建自定义单元格作为定义


快乐编码..

许多人使用添加标签作为单元格子视图的技术,并且可能在重用单元格时遇到意外结果。苹果已经提供了可以在各个方面定制的模板。 在您的情况下,如果不使用自定义单元格,也不添加标签,我将使用模板
UITableViewCellStyleValue2
,您可以使用现有标签和附件视图,如
cell.textLabel
cell.detailTextLabel
cell.accessoryView
,请参阅这个或多或少模拟您的接口的代码段:

- (UITableViewCell *)tableView:(UITableView *)tableView 
                     cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellID = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
        reuseIdentifier:CellIdentifier] autorelease];
    }

        cell.textLabel.contentMode=UIViewContentModeScaleToFill;
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
        cell.textLabel.textAlignment=UITextAlignmentCenter;
        cell.textLabel.font=[UIFont boldSystemFontOfSize:22];
        cell.textLabel.textColor=[UIColor lightGrayColor];

        cell.detailTextLabel.contentMode=UIViewContentModeScaleToFill;
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.baselineAdjustment=UIBaselineAdjustmentAlignCenters;
        cell.detailTextLabel.textAlignment=UITextAlignmentLeft;
        cell.detailTextLabel.font=[UIFont boldSystemFontOfSize:23];
        cell.detailTextLabel.textColor=[UIColor blackColor];

        cell.textLabel.text=@"Tel.:";
        cell.detailTextLabel.text=@"+3912345678";

        UIImageView *imageView = [[UIImageView alloc] initWithImage:
                                  [UIImage imageNamed:@"phone.png"]];
        cell.accessoryView = imageView;
        [imageView release];
        return cell;
}

希望这能有所帮助。

您始终可以将UITableViewCell和两个UILabel子类化。您不应该真正将子视图添加到单元格中,而是添加到单元格的内容视图中,以便编辑行为合理。此外,这还需要调整默认文本标签的框架,以便没有重叠。
for(UIView*eachView in[cell subview])[eachView removeFromSuperview]太棒了+1对于(UIView*eachView in[cell subview].copy)的
更安全。另一个窗体崩溃(或在以前的Cocoa中用于崩溃)。