如何将UILabel的属性反映到另一个?

Sar*_*ith 10 xcode objective-c ios swift

我试图UITableViewController动态定制 .所以我改变了许多属性cell.textLabel.现在我想将这些属性复制detailTextLabel到我通过代码创建的一个标签.怎么做?

    cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
    cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
Run Code Online (Sandbox Code Playgroud)

这是我的cellForRowAtIndexPath

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.textLabel.text=[_names objectAtIndex:indexPath.row];
        cell.textLabel.tag=indexPath.row;

        cell.detailTextLabel.text=[_phones objectAtIndex:indexPath.row];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"] ];
        [imageView setFrame:CGRectMake(380,10,30,50)];
        [cell addSubview:imageView];
        //customize the seperator
        UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1)];/// change size as you need.
        separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
        [cell.contentView addSubview:separatorLineView];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
        cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
        cell.textLabel.textColor=[UIColor whiteColor];
        cell.textLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
        cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
        //here i want to copy the properties
        return cell;
    }
Run Code Online (Sandbox Code Playgroud)

Jan*_*aya 2

您可以使用此方法将所有标签设为UITabelViewCell同一属性

这里只是循环遍历子视图并检查子视图是否为UILabel,如果是UILabel则设置你想要的属性。

我的代码:

- (void)formatTheLabelForCell:(UITableViewCell *)cell
{
    for (UIView *view in cell.contentView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *lbl = (UILabel *)view;

            lbl.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
            lbl.textColor=[UIColor whiteColor];
            lbl.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
            lbl.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)