ssnielsen ssnielsen - 1 year ago 163
Swift Question

UILabel subclass appearance in Storyboard

I have created a subclass of UILabel called MyUILabel. The only thing changed is the font and font-size. It appears as expected when I run the app. However, the in the Storyboard, the default UILabel is showed. Is there any way to make Storyboards show the font and font-size from my subclass?


public class MyUILabel : UILabel {
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.font = UIFont(name: Constants.DefaultFont, size: 30)

Rob Rob
Answer Source

You could make it @IBDesignable, and then implement prepareForInterfaceBuilder:

public class MyUILabel: UILabel {

    public override func awakeFromNib() {

    public override func prepareForInterfaceBuilder() {

    func configureLabel() {
        font = UIFont(name: Constants.DefaultFont, size: 40)


Note, IB didn't like it when I implemented init(coder:), so I moved it into awakeFromNib.

Also note that when you make an @IBDesignable class, Apple advises that you create a separate target (e.g. "File" - "New" - "Target..." - "Cocoa Touch Framework") for this designable class. For more information, see WWDC 2014 video What’s New in Interface Builder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download