Weird issue while using IBDesignable UILabel class

杀马特。学长 韩版系。学妹 提交于 2019-12-12 04:47:46

问题


I am facing a weird issue when I try to use the custom UILabel that I have designed. The view is visible in the storyboard and its working fine with properties. Now What I have done is in my Designable class i have set a property called isError which when set, I need to append an * at the start of my text.

But as soon as in my code I do that, My Designable properties are not used and the Label is not correctly shown on the device and it takes the default properties of UILabel without adding * to the text. Not sure where I am going wrong.

Custom Label Code

@IBDesignable class KGIBDesignableLabel: UILabel {

    @IBInspectable var verticalPad: CGFloat = 0
    @IBInspectable var horizontalPad: CGFloat = 0
    var isError: Bool = false{
        didSet {
            setup()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        setup()
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }


    func setup(){
        if isError{
            text="*"+text!;
            textColor = UIColor.KGColorPalette.errorMessageColor
        }else{
            textColor = UIColor.KGColorPalette.textEntryLabelColor
            text=text!;
        }

        font = UIFont(name: "Helvetica", size: 14)
        clipsToBounds = true
        textAlignment = .center
        numberOfLines = 0
        lineBreakMode = NSLineBreakMode.byWordWrapping

        sizeToFit()
    }

    override var intrinsicContentSize: CGSize {
        let superSize = super.intrinsicContentSize
        let newWidth = superSize.width + superSize.height + (2 * horizontalPad)
        let newHeight = superSize.height + (2 * verticalPad)
        let newSize = CGSize(width: newWidth, height: newHeight)
        return newSize
    }
}

Access code in VC

class ViewController: UIViewController {
    @IBOutlet weak var labelCustom: KGIBDesignableLabel!



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        labelCustom.isError=true
        // After adding this^ line it takes default UILabel properties
    }

回答1:


First Mistake calling setup in your layoutSubview method cause a consecutive calling from setup to layoutSubview because you are modifying things in the setup that cause layoutSubview is called

Fix Remove your setup method from layoutSubviews()

Second Mistake calling sizeToFit() in your setup adjust the size of your text to the current size before calculate your intrinsic content size

Fix remove the sizeToFit() from your setup

Third Mistake you were setting as width the label.intrinsicSize.width + label.intrinsicSize.height + (2 * horizontalPad) is obvious that height is wrong there

Fix replace this line let newWidth = superSize.width + superSize.height + (2 * horizontalPad) by this one `let newWidth = superSize.width + (2 * horizontalPad)

your code modified and working

@IBDesignable class KGIBDesignableLabel: UILabel {

    @IBInspectable var verticalPad: CGFloat = 0
    @IBInspectable var horizontalPad: CGFloat = 0
    var isError: Bool = false{
        didSet {
            setup()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        setup()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }


    func setup(){
        if isError{
            text="*"+text!;
            textColor = UIColor.red
        }else{
            textColor = UIColor.black
            text=text!;
        }

        font = UIFont(name: "Helvetica", size: 14)
        clipsToBounds = true
        textAlignment = .center
        numberOfLines = 0
        lineBreakMode = NSLineBreakMode.byWordWrapping
    }

    override var intrinsicContentSize: CGSize {
        let superSize = super.intrinsicContentSize
        let newWidth = superSize.width + (2 * horizontalPad)
        let newHeight = superSize.height + (2 * verticalPad)
        let newSize = CGSize(width: newWidth, height: newHeight)
        return newSize
    }
}

this is how it looks with 10 and 10 as values por vertical and horizontal padding

hope this helps



来源:https://stackoverflow.com/questions/45408409/weird-issue-while-using-ibdesignable-uilabel-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!