-2

I have a bunch of same labels and created UILabel subclass to customise them. But changes made in CustomizedLabel are not applied to labels in view controller. Here is my code:

import UIKit

class CustomizedLabel: UILabel {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    func commonInit(){
        layer.cornerRadius = 10
        textColor = UIColor.green
        layer.borderColor = UIColor.green.cgColor
        layer.borderWidth = 1
    }
    
}

And view:

import UIKit

class StatiscticsScreenViewController: UIViewController {
    
    @IBOutlet weak var threeSymbTimeRecordLabel: CustomizedLabel!
    @IBOutlet weak var fourSymbTimeRecordLabel: CustomizedLabel!
    @IBOutlet weak var fiveSymbTimeRecordLabel: CustomizedLabel!
    @IBOutlet weak var sixSymbTimeRecordLabel: CustomizedLabel!
    @IBOutlet weak var sevenSymbTimeRecordLabel: CustomizedLabel!
    
    @IBOutlet weak var ThreeSymbMovesRecordLabel: CustomizedLabel!
    @IBOutlet weak var FourSymbMovesRecordLabel: CustomizedLabel!
    @IBOutlet weak var FiveSymbMovesRecordLabel: CustomizedLabel!
    @IBOutlet weak var SixSymbMovesRecordLabel: CustomizedLabel!
    @IBOutlet weak var SevenSymbMovesRecordLabel: CustomizedLabel!
    
    ...

Unfortunately for now I don't have possibility for voting for comment but first comment from Cristik actually helped me. I just forgot to change class on Storybord

harasym
  • 1
  • 2
  • 4
    Are the views defined as `CustomizedLabel` in the storyboard/xib? – Cristik Aug 31 '21 at 11:14
  • 1
    first, make sure commonInit() method is running – Ryan110 Aug 31 '21 at 11:20
  • make sure of you'd apply those visual changes (`commonInit()`) at the right point of the view lifecycle, eg. when it has a `superview` or when the layout needs to be updated (ie. `layoutSubviews()`). – holex Aug 31 '21 at 11:55
  • 1
    OMG ))) many thanks @Cristik )))) – harasym Aug 31 '21 at 12:00
  • 1
    @Ryan110 thanks for reply. I just forgot to change class to created subclass on storyboard. – harasym Aug 31 '21 at 12:03
  • @harasym in that case I think you can delete the question – Cristik Aug 31 '21 at 18:18
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 03 '21 at 09:27

2 Answers2

0

IBOutlet are not created via the init. To Configure your custom label, make changes in awakeFromNib method and not in the init of UILabel class.

  • What do you mean they are not created? `required init?(coder aDecoder:` this init gets called when the view unarchived from `nib/xib` file file. – vpoltave Aug 31 '21 at 14:10
-1

you are creating your labels from storyboard, not programmatically. so, you should try this one

import UIKit

class CustomizedLabel: UILabel {

    
    func commonInit(){
        layer.cornerRadius = 10
        textColor = UIColor.green
        layer.borderColor = UIColor.green.cgColor
        layer.borderWidth = 1
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        commonInit()
    }
    
}
  • This is incorrect, see: https://stackoverflow.com/questions/38386339/what-exactly-is-init-coder-adecoder/38386580 – vpoltave Aug 31 '21 at 14:40