UILabel子类化子类不会显示

问题描述 投票:1回答:1

我正在尝试创建一个自定义UILabel,我的第一个子类正在按预期工作,我做了一些字体属性修改和更改。

@IBDesignable class LunaLbl: UILabel {
    @IBInspectable dynamic open var fontSize: CGFloat = 25 {
        didSet {
            updateFont()
        }
    }
    @IBInspectable dynamic open var fontName: String = Fonts.proDisplayBold.rawValue {
        didSet {
            updateFont()
        }
    }
    @IBInspectable dynamic open var fontColor: UIColor = .lunaBlack {
        didSet {
            updateFont()
        }
    }
    @IBInspectable dynamic open var lineSpacing:  CGFloat = 1.2 {
        didSet {
            updateFont()
        }
    }
    override open func draw(_ rect: CGRect) {
        super.draw(rect)
        updateFont()
        self.adjustsFontSizeToFitWidth = false
    }

    func updateFont() {
        let textContent = self.text
        let textString = NSMutableAttributedString(string: textContent!, attributes: [
            NSAttributedString.Key.font: UIFont(name: fontName, size: fontSize)!])
        let textRange = NSRange(location: 0, length: textString.length)
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = lineSpacing
        textString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: textRange)
        textString.addAttribute(NSAttributedString.Key.foregroundColor, value: fontColor, range: textRange)
        self.attributedText = textString
    }
}

现在我继承自第一个子类的第二个子类在我的屏幕上没有显示任何内容。

@IBDesignable class LunaTitleLbl : LunaLbl {
    override open func draw(_ rect: CGRect) {
        super.draw(rect)
        fontSize = 25.0
        fontName = Fonts.proDisplayBold.rawValue
        fontColor = .lunaBlack
        updateFont()
        self.adjustsFontSizeToFitWidth = false
    }
} 

我做同样的事情,我直接从故事板继承我的新子类,但是当我使用视图层次调试它时没有显示,我的标签被创建并且它采用我想要的值,但文本没有显示。

多数民众赞成我如何继承我的子类enter image description here

swift uilabel
1个回答
0
投票

当我更改属性名称lineSpacing并且所有内容都按预期工作时,我认为问题是我使用相同的标签属性名称。

© www.soinside.com 2019 - 2024. All rights reserved.