Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make @IBDesignable override Interface Builder values?

For example, I want to subclass UIButton and set it's font to 20.0f by default. I can write something like this:

@IBDesignable

class HCButton: UIButton {
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.customInit()
  }

  func customInit () {
    titleLabel?.font = UIFont.systemFontOfSize(20)
  }
} 

But this does not affect preview in Interface Builder, all custom buttons appear with 15.0f font size by default. Any thoughts?

like image 301
orkenstein Avatar asked Aug 13 '15 12:08

orkenstein


1 Answers

I have created new IBInspectable as testFonts :

import UIKit

@IBDesignable

class CustomButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.customInit()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.customInit()
    }

    func customInit () {
        titleLabel?.font = UIFont.systemFontOfSize(20)
    }

    convenience init() {
        self.init(frame:CGRectZero)
        self.customInit()
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.customInit()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        self.customInit()
    }
}

Hope it helps you :)

This is working for me.

like image 152
Ashish Kakkad Avatar answered Nov 15 '22 08:11

Ashish Kakkad