How do copy for UILabel?

后端 未结 5 1447
星月不相逢
星月不相逢 2020-12-03 08:21

I have IBOutlet UILabel *label;

and I want to do this

UILabel *label = [titleLabel copy];
label.text = @\"Clone\";
titleLabel.text = @         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 08:33

    There is no public Apple API to deep copy a UILabel. Your best bet is to make a helper method which copies all the parts you care about.

    - (UILabel *)deepLabelCopy:(UILabel *)label {
        UILabel *duplicateLabel = [[UILabel alloc] initWithFrame:label.frame];
        duplicateLabel.text = label.text;
        duplicateLabel.textColor = label.textColor;
        // etc... anything else which is important to your ULabel
    
        return [duplicateLabel autorelease];
    }
    

    If you want to use it all over your code base you can change it to a static method and put it in some sort of utility class. If you named the class LabelUtils you could do something like...

    + (UILabel *)deepLabelCopy(UILabel *)label {
        // ...
    }
    

    and would be called using UILabel *dupLabel = [LabelUtils deepLabelCopy:origLabel];

提交回复
热议问题