简体   繁体   English

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

[英]Weird issue while using IBDesignable UILabel class

I am facing a weird issue when I try to use the custom UILabel that I have designed. 当我尝试使用我设计的自定义UILabel时,我遇到一个奇怪的问题。 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. 现在,我要做的是在我的Designable类中设置了一个名为isError的属性,设置该属性时,需要在文本开头添加*。

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. 但是,只要在我的代码中这样做,就不会使用My Designable属性,并且Label也不会正确显示在设备上,并且它将采用UILabel的默认属性,而不会在文本中添加*。 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 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
    }

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 在您的layoutSubview方法中的第一个错误调用setup会导致从setuplayoutSubview的连续调用,因为您正在修改setup中导致layoutSubview被调用的内容

Fix Remove your setup method from layoutSubviews() 修复layoutSubviews()删除您的setup方法

Second Mistake calling sizeToFit() in your setup adjust the size of your text to the current size before calculate your intrinsic content size 在您的setup 第二次错误调用sizeToFit()将文本的大小调整为当前大小,然后计算内部内容大小

Fix remove the sizeToFit() from your setup 修复从您的setup删除sizeToFit()

Third Mistake you were setting as width the label.intrinsicSize.width + label.intrinsicSize.height + (2 * horizontalPad) is obvious that height is wrong there 第三个错误是您将label.intrinsicSize.width + label.intrinsicSize.height + (2 * horizontalPad)设置为width ,很明显那里的高度是错误的

Fix replace this line let newWidth = superSize.width + superSize.height + (2 * horizontalPad) by this one `let newWidth = superSize.width + (2 * horizontalPad) 修复替换此行, let newWidth = superSize.width + superSize.height + (2 * horizontalPad)替换为这一个`let newWidth = superSize.width +(2 * horizo​​ntalPad)

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 这是使用10和10作为垂直和水平填充值时的外观

在此处输入图片说明

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM