xcode UILabel 如何复制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8712866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:53:04  来源:igfitidea点击:

How do copy for UILabel?

iphoneiosxcodeuikitnsobject

提问by Igor Bidiniuc

I have IBOutlet UILabel *label;

我有 IBOutlet UILabel *label;

and I want to do this

我想这样做

UILabel *label = [titleLabel copy];
label.text = @"Clone";
titleLabel.text = @"Original";
NSLog(@"label : %@, title : %@",label.text,titleLabel.text);

and this throw exception

而这个抛出异常

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel copyWithZone:]: unrecognized selector sent to instance 0x6a4a450' *First throw call stack: (0x126f052 0x1823d0a 0x1270ced 0x11d5f00 0x11d5ce2 0x1271bd9 0x2ed6 0x1270e1a 0x2851 0x28264e 0x1e2a73 0x1e2ce2 0x1e2ea8 0x1e9d9a 0x24af 0x1ba9d6 0x1bb8a6 0x1ca743 0x1cb1f8 0x1beaa9 0x215cfa9 0x12431c5 0x11a8022 0x11a690a 0x11a5db4 0x11a5ccb 0x1bb2a7 0x1bca9b 0x21c2 0x2135)

*终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因是: ' - [的UILabel copyWithZone:]:无法识别的选择发送到实例0x6a4a450' *第一掷调用堆栈:(0x126f052 0x1823d0a 0x1270ced 0x11d5f00 0x11d5ce2 0x1271bd9 0x2ed6 0x1270e1a 0x2851 0x28264e 0x1e2a73 0x1e2ce2 0x1e2ea8 0x1e9d9a 0x24af 0x1ba9d6 0x1bb8a6 0x1ca743 0x1cb1f8 0x1beaa9 0x215cfa9 0x12431c5 0x11a8022 0x11a690a 0x11a5db4 0x11a5b4 0x10x7b20cb4 0x10x1b20cc1b2a5c20cc1

回答by DBD

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.

没有公开的 Apple API 来深度复制 UILabel。最好的办法是制作一个辅助方法来复制您关心的所有部分。

- (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 LabelUtilsyou could do something like...

如果您想在整个代码库中使用它,您可以将其更改为静态方法并将其放入某种实用程序类中。如果你给这个班级命名,LabelUtils你可以做这样的事情......

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

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

并且会被调用使用 UILabel *dupLabel = [LabelUtils deepLabelCopy:origLabel];

回答by itsame69

I recommend using a merged version of Answer 1 and Answer 2:

我建议使用答案 1 和答案 2 的合并版本:

- (UILabel *)copyLabel:(UILabel *)label {
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: label];
    UILabel* copy = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
    return copy;
}

Then simply use something like

然后简单地使用类似的东西

UILabel* labelcopy = [self copyLabel:originalLabel];

in your code.

在你的代码中。

回答by KIO

UILabel does not conform to NSCopying, so you cannot make a copy via -copy.

UILabel 不符合 NSCopying,因此您无法通过 -copy 进行复制。

But it does conform to NSCoding, so you can archive the current instance, then unarchive a 'copy'.

但它确实符合 NSCoding,因此您可以存档当前实例,然后取消存档“副本”。

NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: label];
UILabel *labelCopy =   [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];

Afterwards, you'll have to assign any additional properties that weren't carried over in the archive (e.g. the delegate) as necessary.

之后,您必须根据需要分配存档中未携带的任何其他属性(例如委托)。

回答by Mark Moeykens

Swift 3 Solution

Swift 3 解决方案

I created it as an extension to the UILabel.

我创建它作为UILabel.

extension UILabel {
    func createCopy() -> UILabel {
        let archivedData = NSKeyedArchiver.archivedData(withRootObject: self)
        return NSKeyedUnarchiver.unarchiveObject(with: archivedData) as! UILabel
    }
}

How to use

如何使用

let anotherNameLabel = nameLabel.createCopy()

Additional Info - How does this work?

附加信息 - 这是如何工作的?

NSKeyedArchiverwill take an object from memory and convert it to text. "Keyed" means it uses words to describe the properties of this object, like "LabelFrame", "LabelText", etc. (These are fake keys.) This also called Serialization in other technologies.

NSKeyedArchiver将从内存中取出一个对象并将其转换为文本。“Keyed”是指用文字来描述这个对象的属性,比如“LabelFrame”、“LabelText”等(这些是假键)。这在其他技术中也称为序列化。

NSKeyedUnarchiverdoes the opposite. It will take that text and build an object. Here we take that object (Any?) and convert it to a UILabel.

NSKeyedUnarchiver反其道而行之。它将获取该文本并构建一个对象。在这里,我们将该对象 ( Any?) 转换为 a UILabel

回答by Fred Faust

I solved it this way and wanted to post it for easy copy and pasting if you were thinking of doing the same.

我以这种方式解决了它,并希望将其发布以方便复制和粘贴,如果您也想这样做的话。

func makeCopy() -> UILabel {

  let label = UILabel(frame: frame)
  label.text = text
  label.font = font
  label.textColor = textColor
  label.shadowColor = shadowColor
  label.shadowOffset = shadowOffset
  label.textAlignment = textAlignment
  label.lineBreakMode = lineBreakMode
  label.attributedText = attributedText
  label.highlightedTextColor = highlightedTextColor
  label.isHighlighted = isHighlighted
  label.isUserInteractionEnabled = isUserInteractionEnabled
  label.isEnabled = isEnabled
  label.numberOfLines = numberOfLines
  label.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth
  label.baselineAdjustment = baselineAdjustment
  label.minimumScaleFactor = minimumScaleFactor
  label.allowsDefaultTighteningForTruncation = allowsDefaultTighteningForTruncation
  label.preferredMaxLayoutWidth = preferredMaxLayoutWidth

  return label
}