The emergence of the UILabel subclass in the storyboard

I created a subclass of UILabel called MyUILabel. The only thing that has changed is the font and font size. It appears as expected when I launch the application. However, the storyboard displays UILabel by default. Is there a way to make Storyboards display font and font size from my subclass?

MyUILabel:

public class MyUILabel : UILabel { required public init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.font = UIFont(name: Constants.DefaultFont, size: 30) } } 
+4
source share
1 answer

You can do this @IBDesignable and then implement prepareForInterfaceBuilder :

 @IBDesignable public class MyUILabel: UILabel { public override func awakeFromNib() { super.awakeFromNib() configureLabel() } public override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() configureLabel() } func configureLabel() { font = UIFont(name: Constants.DefaultFont, size: 40) } } 

Note. IB did not like it when I implemented init(coder:) , so I moved it to awakeFromNib .

Also note that when you create the @IBDesignable class, Apple advises creating a separate target (for example, "File" - "New" - "Target ..." - "Cocoa Touch Framework") for this conditional class. For more information, see WWDC 2014 Video What's New in Interface Builder .

+7
source

All Articles