3

I've created a subclass to manage my Theme but is not showing neither on device or simulator.

Here my Header.swift:

import Foundation
import UIKit

class Header: UILabel {
    override var textColor: UIColor! {
        // White Color
        get { return ThemeManager.currentTheme.palette.primary }
        set {}
    }

    override var font: UIFont! {
        get { return ThemeManager.currentTheme.textStyle.headerText }
        set {}
    }
}

Here the implementation: (inside the viewcontroller)

var titleLabel: Header = Header()

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .black

    // Background Image over the view
    setupBackground()

    setupStartButton()
    setupTitleLabel()

    print(titleLabel.frame)
}

// MARK: - Header
private func setupTitleLabel() {
    titleLabel.text = "0.0m"

    // titleLabel.font = ThemeManager.currentTheme.textStyle.headerText
    // titleLabel.textColor = ThemeManager.currentTheme.palette.primary

    view.addSubview(titleLabel)
    view.bringSubviewToFront(titleLabel)

    setupTitleLabelAutolayout()
}


private func setupTitleLabelAutolayout() {
    titleLabel.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        titleLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor)
    ])
}

But if I use UILabel instead of Header it works perfectly as expected. I've also tried to implement init?(coder aDecoder: NSCoder) and init(frame: CGRect) but nothing changed.

If I set a frame on init then shows the text, but not styled and ignoring my constraints.

Surely I'm missing something, but what?

To avoid usefulness answers, here some infos:

  • The UILabel textColor is white
  • The background is black and has an image over it.
  • I've tried to remove the image and all the stuff around except for the label and nothing changed.
4

2 Answers 2

3

That's a poor reason to use subclassing. It doesn't allow you to mix-and-match when appropriate.

Better would be to make an extension:

extension UILabel {
    func withHeaderStyle() -> UILabel {
        self.textColor = ThemeManager.currentTheme.palette.primary
        self.font = ThemeManager.currentTheme.textStyle.headerText
        return self
    }
}

Then at point of use:

var titleLabel = UILabel().withHeaderStyle()

You can make several of these "withXStyle" methods and at the point of use you can chain them together. That's something you can't do with inheritance.


In general you should only use inherence when you want to change behavior. It's ill suited for changing data.

1

I've fixed that by editing the Header to this:

import Foundation
import UIKit

class Header: UILabel {
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupStyle()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setupStyle()
    }

    private func setupStyle() {
        self.textColor = ThemeManager.currentTheme.palette.primary
        self.font = ThemeManager.currentTheme.textStyle.headerText
    }
}

Basically if I understood right, when I set the getter in the label it doesn't (if you think about, it's quite obvious) anything.

I still think that there are better solutions, but this works fine for me so, I'll keep that.

Now you may ask: "Why did you overwritten the getter instead of doing this?"

It's the right question, and the right answer is that I read it in a swift article on medium, so I tought it was right.

PS: I've also tried with didSet but it obviously loop through it self and crash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.