使用IBDesignable UILabel类时出现奇怪的问题

时间:2017-07-31 05:58:51

标签: ios swift3

当我尝试使用我设计的自定义UILabel时,我遇到了一个奇怪的问题。该视图在故事板中可见,其工作正常。现在我所做的是在我的Designable类中,我设置了一个名为isError的属性,在设置时,我需要在文本的开头添加*。

但是,只要在我的代码中执行此操作,就不会使用My Designable属性,并且Label未在设备上正确显示,并且它在不向文本添加*的情况下采用UILabel的默认属性。不知道我哪里出错了。

自定义标签代码

@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
    }
}

访问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 个答案:

答案 0 :(得分:1)

第一次错误setup方法中调用layoutSubview导致从setuplayoutSubview的连续调用,因为您正在修改setup中的内容导致layoutSubview的{​​1}}被称为

修复setup

中删除layoutSubviews()方法

第二次错误sizeToFit()中调用setup,在计算内在内容大小之前,将文字大小调整为当前大小

修复sizeToFit()

中删除setup

第三个错误您设置为width label.intrinsicSize.width + label.intrinsicSize.height + (2 * horizontalPad)显然高度错误

修复将此行替换为let newWidth = superSize.width + superSize.height + (2 * horizontalPad)`let newWidth = superSize.width +(2 * horizo​​ntalPad)

您的代码已修改正常工作

@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
    }
}

这是10和10看起来如何作为垂直和水平填充的值

enter image description here

希望这会有所帮助