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

10

我正在尝试动态自定义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;

这是我的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;
    }

你能展示一下你的cellForRowAtIndexPath方法吗? - Anbu.Karthik
创建一个方法来检索这些信息并将其应用于另一个。 - Larme
2
不要在单元格中像那样添加子视图,因为单元格会被重用。 - Larme
这只是一个例子。 - Saranjith
所以你正在从网络/服务器加载图像吗?如果是,你是同步还是异步进行的? - Arpit Dongre
显示剩余8条评论
10个回答

4

对于Swift3

class MyLabel: UILabel {
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        backgroundColor = UIColor(red: 0, green: 0.188235, blue: 0.313725, alpha: 1)
        textColor = UIColor.white
        font = UIFont(name: "HelveticaNeue", size: 26)
        autoresizingMask = .flexibleRightMargin
    }
}

以这种方式创建UILabel的子类。
#import "MyLabel.h"

@implementation MyLabel

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    self.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
    self.textColor=[UIColor whiteColor];
    self.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
    self.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}

@end

现在创建一个MyLabel对象,你的属性将自动设置,并将此类分配给单元格中标签的storyboard。

子类化是实现可重用代码的最佳方式。

或者您甚至可以创建一个类的扩展或类方法,在某个类中接受UILabel并设置属性,但这些都不是最佳实践。扩展的另一个问题是只能使用self而不能使用super。当您必须扩展属性时,这可能会在未来创建问题。

我希望我清楚且有帮助。


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;
        }
    }
}

它运行良好。我想知道是否有任何代码可以动态地在XCode编辑器中复制和粘贴工具。 - Saranjith
我从“cellForRowAtIndexPath”调用了您的“formatTheLabelForCell”,但是我的“ViewController”中有许多行,大约100行,所以需要更长的时间。每次创建单元格时都要循环浏览视图。 - Saranjith
不建议同时加载这么多单元格。即使您没有使用“加载更多”,您是否在单个时间内显示了100个单元格? - Janmenjaya
是的,我有。那是客户的需求。 - Saranjith

2

不要在 cellForRowAtIndexPath 中配置单元格,最好使用自定义单元格。在 Storyboard/Nib 中添加 imageViewseparatorLineView。这样所有的单元格都将使用这些默认属性生成。如果需要通过代码进行配置,可以在 CustomCell.m 文件中编写如下代码:

 class CustomCell: UITableViewCell {
    override func awakeFromNib() {
    super.awakeFromNib()
    self.textLabel.backgroundColor = UIColor.redColor
    //do other configurations
    }

编辑:从网上下载图像可能是加载单元格需要很长时间的原因。尝试异步下载图像。您也可以使用此库:SDWebImage

注意:我知道您想要Objective C版,上面的Swift代码仅供说明。


2
关于Swift,您可以这样做,它将复制您应用于textLabel的所有属性到detailTextLabel中。
 cell.detailTextLabel.attributedText = cell.textLabel.attributedText

2

首先,我认为iOS SDK中没有复制特定属性的任何UIKit组件的功能。因此,您将不得不编写自定义函数来实现此功能。此外,如其他人在评论中指出,您的“cellForRowAtIndexPath”存在一些问题。

这个问题有不同的解决方法。

解决方法1: 在您的视图控制器中编写一个函数,该函数以两个标签作为参数,并复制所需的值。

-(void)copyPropertiesFrom:(UILabel*)label1 toLabel:(UILabel*)label2{
    label2.backgroundColor  = label1.backgroundColor;
    label2.textColor        = label1.textColor;
    label2.font             = label1.font;
    label2.autoresizingMask = label1.autoresizingMask;
}

在cellForRowAtIndexPath方法中,如果你想要复制,请按照以下步骤操作。
[self copyPropertiesFrom:cell.titleLabel toLabel:cell.detailTextLabel];
解决方案2(推荐):在我的经验中,这是最好的方法,因为您可以在其他视图控制器中重复使用它。可能有比这更好的方法。
创建一个UILabel类别。请查看此链接如何在Xcode 6或更高版本中创建类别?https://code.tutsplus.com/tutorials/objective-c-categories--mobile-10648
您在类别中的函数将如下所示。
-(void)formatLabelToMyStyle{
    self.backgroundColor    = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
    self.textColor          = [UIColor whiteColor];
    self.font               = [UIFont fontWithName:@"HelveticaNeue" size:26];
    self.autoresizingMask   = UIViewAutoresizingFlexibleRightMargin;
}

您需要在cellForRowAtIndexPath中包含类别的头文件并调用此函数,如下所示:

您需要在cellForRowAtIndexPath中包含类别的头文件并调用此函数,如下所示

[cell.titleLabel formatLabelToMyStyle];
[cell.detailTextLabel formatLabelToMyStyle];
[cell.customTextLabel formatLabelToMyStyle];

至于你的cellForRowAtIndexPath,larme在评论中提到“不要像这样在单元格中添加子视图,因为单元格会被重用”。这将不断向您的单元格添加视图,从而引起内存问题,特别是当您有大量单元格时,这在您的情况下是正确的。


2
你可以使用 Category 来为 UILabel 添加样式,或者创建一个 UILabel 的子类来共享相同的样式。

一个 UILabelCategory 可以像这样:

// UILabel+CustomStyle.h
#import <UIKit/UIKit.h>

@interface UILabel (CustomStyle)
-(void) applyCustomStyle;
@end

.m 文件:

// UILabel+CustomStyle.m
#import "UILabel+CustomStyle.h"

@implementation UILabel (CustomStyle)

-(void) applyCustomStyle {
    self.backgroundColor = [UIColor colorWithRed: 0 green: 0.188235 blue: 0.313725 alpha: 1];
    self.textColor = [UIColor whiteColor];
    self.font = [UIFont fontWithName: @"HelveticaNeue" size: 26];
    self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}

@end

然后,您只需调用相同的样式即可应用:

#import "UILabel+CustomStyle.h"
[label applyCustomStyle];

2
如果您希望在项目中的多个地方使用相同的标签配置,只需像@NikhilManapure所说的那样进行子类化即可。
或者,如果您想要将相同的属性应用于TableViewCell的textLabel和detailTextLabel。您应该对TableViewCell进行子类化,并在drawrect方法中覆盖Label属性。
Objective-C
        #import <UIKit/UIKit.h>

        @interface PropertiesCell : UITableViewCell

        @end

        #import "PropertiesCell.h"

        @implementation PropertiesCell

        - (void)awakeFromNib {
            [super awakeFromNib];
            // Initialization code
        }

        - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
            [super setSelected:selected animated:animated];

            // Configure the view for the selected state
        }

        - (void)drawRect:(CGRect)rect {
            [super drawRect:rect];
            [self cellLabelConfigure:self.textLabel];
            [self cellLabelConfigure:self.detailTextLabel];
        }

        - (void)cellLabelConfigure:(UILabel*) contentLabel {
            contentLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
            contentLabel.textColor=[UIColor whiteColor];
            contentLabel.font=[UIFont fontWithName:@"HelveticaNeue" size:26];
            contentLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
        }

        @end

Swift

 class PropertiesCell: UITableViewCell {
    override func draw(_ rect: CGRect) {
        super.draw(rect)
        cellLabelsConfigure(contentLabel: self.textLabel)
        cellLabelsConfigure(contentLabel: self.detailTextLabel)
    }

    func cellLabelsConfigure(contentLabel: UILabel?) {
        contentLabel?.backgroundColor = UIColor(red: 0.0, green: 0.188, blue: 0.313, alpha: 1.0)
        contentLabel?.textColor = UIColor.white
        contentLabel?.font = UIFont(name: "HelveticaNeue", size: 26.0)
        contentLabel?.autoresizingMask = UIViewAutoresizing.flexibleRightMargin
    }
}

在故事板中将单元格类名称更改为 PropertiesCell

enter image description here


1
创建一个扩展类并使用此copy方法将您想要传递到新标签的所有属性传递过去。
@implementation UILabel (Copy)

- (UILabel *)copyProperties {
    UILabel *label = [UILabel new];
    [self copyPropertiesWithLabel:label];
    return label;
}

- (void)copyPropertiesWithLabel:(UILabel *)label {
    label.backgroundColor = self.backgroundColor;
    label.textColor = self.textColor;
    label.font = self.font;
    label.autoresizingMask = self.autoresizingMask;
    // Add more properties
}

@end

使用方法:

// cell.textLabel has now all the properties
[theLabelToBeCopied copyPropertiesWithLabel:cell.textLabel];

1

Swift3版本:

extension UILabel {

    func copyProperties() -> UILabel {
        var label = UILabel()
        self.copyProperties(with: label)
        return label
    }

    func copyProperties(with label: UILabel) {
        label.backgroundColor = self.backgroundColor
        label.textColor = self.textColor
        label.font = self.font
        label.autoresizingMask = self.autoresizingMask
        // Add more properties
    }
}

使用方法:

theLabelToBeCopied.copyProperties(with: cell.textLabel)

0

你可以用 Swift 做到这一点。它将复制所有属性(从 textLabel 到 detailTextLabel)。我认为 @Nikhil Manapure 给出了确切的答案。

cell.detailTextLabel.attributedText = cell.textLabel.attributedText

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接