Ios 自定义UILabel未在Interface Builder中正确显示

Ios 自定义UILabel未在Interface Builder中正确显示,ios,swift,interface-builder,uilabel,Ios,Swift,Interface Builder,Uilabel,我有一个自定义的基于UILabel的类,它不能在情节提要中正确显示 它只显示一个空白标签。XCode 6和7测试版都会出现这种情况 import UIKit @IBDesignable class CCLabel: UILabel { @IBInspectable var spacingChar: CGFloat = 0 { didSet { self.updateAttributed(self.text) } } @IBInspectable var

我有一个自定义的基于UILabel的类,它不能在情节提要中正确显示

它只显示一个空白标签。XCode 6和7测试版都会出现这种情况

import UIKit

@IBDesignable


class CCLabel: UILabel {

@IBInspectable var spacingChar: CGFloat = 0 {
    didSet {
        self.updateAttributed(self.text)
    }
}

@IBInspectable var extraLineSpacing: CGFloat = 0 {
    didSet {
        self.updateAttributed(self.text)
    }
}

override var bounds: CGRect {
    didSet {
        if (bounds.size.width != self.bounds.size.width) {
            self.setNeedsUpdateConstraints()
        }
        super.bounds = bounds
    }
}

override func updateConstraints() {
    if (self.preferredMaxLayoutWidth != self.bounds.size.width) {
        self.preferredMaxLayoutWidth = self.bounds.size.width
    }
    super.updateConstraints()
}

override var text: String? {
    didSet {
        if let txt = text {
            self.updateAttributed(txt)
        }
    }
}

private func updateAttributed(text: String?) {

    if (text == nil) {
        return
    }

    if (extraLineSpacing > 0 || spacingChar > 0) {

        let attrString = NSMutableAttributedString(string: text!)
        let rng = NSMakeRange(0, attrString.length)

        if (extraLineSpacing > 0) {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = extraLineSpacing
            paragraphStyle.alignment = self.textAlignment

            attrString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:rng)
        }
        if (spacingChar > 0) {
            attrString.addAttribute(NSKernAttributeName, value:spacingChar, range:rng)
        }

        attrString.addAttribute(NSFontAttributeName, value: self.font, range: rng)

        self.attributedText = attrString

    }
}
}
这是IB中的自定义UILabel类:

当我删除自定义类标签的名称时,它会正确显示

当我再次添加它时,它不会消失。然而,下一次它又变白了


当你运行这个项目时,它是完美的。我想XCode没有正确地呈现它,但无法解释原因。

我认为您需要将其称为:


[超级drawRect:rect]

当您将UIView以外的任何子类化时,应该在实现中调用super.draw(…)。()

我认为您必须在自定义类中添加init方法。我认为您需要调用这个:[super-drawRect:rect];